0

Django に HTML フォームがあり、Django フォーム クラスを使用していません。

このhtmlフォームをどのように処理できるか知りたいですか?(方法はPOST

フォームはによって生成されxslますが、最終的に生成されるフォームは次のようになります。

<form method="POST" id="settingSubmit" action="/archive/agentUpdate/1">
    <input value="1" name="AgentID" datatype="Int">
    <input value=" agent 1" name="AgentName">
    <input value=" agent 1 Description" name="AgentDescription">
    <input value="submit" id="sendbutton" type="submit">
</form>

とビュー:

def agentUpdate(request,id):
    agentName = request.POST['AgentName']

    return render_to_response('archive/base.html',{
        'agentName':agentName
    },
        RequestContext(request, ))

urls.py:

urlpatterns = patterns('archive.views',

    url(r'^agentUpdate/(?P<id>\w+)/$',
        'agentUpdate',
        name='agent_Update'),
)

エラー:

MultiValueDictKeyError at /archive/agentUpdate/2/

"Key 'AgentName' not found in <QueryDict: {}>"
4

3 に答える 3

0

MultiValueDictKeyErrorあなたがすべきことについて

agentName = request.POST.get('AgentName')

POSTまた、これがリクエストかどうかを確認することをお勧めします

if request.POST:
    agentName = request.POST.get('AgentName')

次に、このデータを使用して、必要なことを何でも実行できます-検証、データベースへの保存、その他の方法での処理。

また、代わりにrender_to_responseショートカットを使用できますrender

return render(request, 'archive/base.html', {'agentName': agentName})
于 2012-09-15T08:48:51.837 に答える
0

フォーム フィールドに追加して、フォームの HTML を修正してくださいtype="hidden"。そうしないと、リクエストの一部として送信されません。

また、保護{% csrf_token %}に必要なものを追加する必要があります。これは、すべてのリクエストでデフォルトで有効になっています。CSRFPOST

<form method="POST" id="settingSubmit" action="/archive/agentUpdate/1">
    {% csrf_token %}
    <input value="1" type="hidden" name="AgentID" datatype="Int">
    <input value=" agent 1" type="hidden" name="AgentName">
    <input value=" agent 1 Description" type="hidden" name="AgentDescription">
    <input value="submit" id="sendbutton" type="submit">
</form>

最後に、あなたの見解では:

from django.shortcuts import render

def agentUpdate(request,id):
    if request.method == 'POST':
        agentName = request.POST.get('AgentName')
        return render(request, 'archive/base.html', {'agentName':agentName})
于 2012-09-17T05:27:56.987 に答える
0

csrf トークンをフォームに追加する

<form method="POST" id="settingSubmit" action="/archive/agentUpdate/1">
    {% csrf_token %}
    <input value="1" type="hidden" name="AgentID" datatype="Int">
    <input value=" agent 1" type="hidden" name="AgentName">
    <input value=" agent 1 Description" type="hidden" name="AgentDescription">
    <input value="submit" id="sendbutton" type="submit">
</form>

views.py に追加

django.shortcuts import render_to_response から

def agentUpdate(request,id):
    if request.method == 'POST':
        agentName = request.POST.get('AgentName')
        variables = RequestContext(request, {'agentName':agentName})
        return render_to_response('archive/base.html', variables)

urls.py で

urlpatterns = patterns('archive.views',

    (r'^agentUpdate/(\w*)\/?$','agentUpdate'),
)
于 2012-09-17T06:45:15.670 に答える