简体   繁体   English

在Django中处理POST请求

[英]Processing POST request in Django

Hi I got a simple form for a POST request and it works when I'm only having one input, but not two inputs together. 嗨,我有一个简单的POST请求表单,当我只有一个输入但没有两个输入时,它就可以工作。 Can someone show me some light on this? 有人可以给我一些启发吗?

index.html 的index.html

<form name="input" action="{% url 'sending' %}" method="post">
{% csrf_token %}
    Recipient: <input type="text" name="recipient"> 
    <br>
    Message: <input type="text" name="content"> 
    <br>
    <input type="submit">
</form>

views.py views.py

def sending(request):
    recipient = request.POST.get('recipient','')
    content = request.POST.get('content','')   #not working when I am doing this...

    someMethod(recipient, content)

    return HttpResponseRedirect(reverse('results'))

Adding a "forms" portion to your setup will help you greatly... see the quickstart docs on forms here: https://docs.djangoproject.com/en/1.6/topics/forms/ 在您的设置中添加“表单”部分将极大地帮助您...在此处查看表单上的快速入门文档: https : //docs.djangoproject.com/en/1.6/topics/forms/

In particular, check out "using a form in a view": https://docs.djangoproject.com/en/1.6/topics/forms/#using-a-form-in-a-view 特别是,请检查“在视图中使用表单”: https : //docs.djangoproject.com/en/1.6/topics/forms/#using-a-form-in-a-view

Basically, you end up with a "forms.py" file which defines your form fields. 基本上,您最终得到一个“ forms.py”文件,该文件定义了表单字段。 Then, after it all processes, you get a simplier API into your form fields that looks like this: 然后,在完成所有过程之后,您将在表单字段中获得一个简单的API,如下所示:

form.cleaned_data['recipient']
form.cleaned_data['content']

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM