升级到django 1.5.1后无法绑定表单数据

时间:2021-10-11 20:18:53

I just upgraded from django 1.4.5 to django 1.5.1, and noticed that all my form processing code stopped working. Specifically, the form data returned with POST cannot be found anymore.

我刚刚从django 1.4.5升级到django 1.5.1,并注意到我的所有表单处理代码都停止了工作。具体来说,无法再找到POST返回的表单数据。

Django code -

Django代码 -

here I follow the instructions from Django 1.5's documentation and pass in the request.POST object after it's been submitted by the user to instantiate the LoginUserForm

在这里,我按照Django 1.5的文档中的说明进行操作,并在用户提交之后传递request.POST对象以实例化LoginUserForm

class UserLoginForm(forms.Form):
    email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Enter email',
                                                       'class': 'span4'}))
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'span4'}))


def login_user(self,request):
    user = None
    if request.method == 'POST':
        print "post:", request.POST.items()
        form = UserLoginForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            email = data['email']
            password = data['password']
            if email and password:
                user_service = UserService()
                email = email.lower()
                password = string_utils.hash_password(password) 
                user = user_service.get_by_email_and_password(password      = password,
                                                              email         = email)
    return user

My form template

我的表单模板

<form class="well" action="/web/user/login_user?next={{ next_url }}" method="post">
{% csrf_token %}
    <label><strong>Email:</strong></label>
        {{ form.email }}
    <label><strong>Password:</strong></label>
        {{ form.password }}
    <br />        
        <div class="row">
            <div class="span4" style="text-align: right;">
            <button type="submit" class="btn">Login</button>
            </div>
        </div>
        <div class="row">
            <div class="span2">
            <a href="/web/forgot_password" class="gray-underline" style="line-height: 25px; font-size: 12px; ">Forgot Password?</a>
            </div>
            <div class="span2" style="text-align: right;">                   
            </div>
        </div>          
</form>

Output of Django.1.4.5 -

Django.1.4.5的输出 -

Django version 1.4.5, using settings 'myproj.settings'
post: [(u'csrfmiddlewaretoken', u'DnRTYpV1EF9XMQRAKoc3u37wya0TS3mX'), (u'password',     u'abcde'), (u'email', u'john@doe.com')]
data: {'password': u'abcde', 'email': u'john@doe.com'}

Output of Django 1.5.1 -

Django 1.5.1的输出 -

Django version 1.5.1, using settings 'myproj.settings'
post: []

I looked at the Django 1.5.1 release notes and noticed there is a part about non-form data not being included in request.POST anymore.

我查看了Django 1.5.1发行说明,并注意到有一部分关于非表单数据不再包含在request.POST中。

Non-form data in HTTP requests request.POST will no longer include data posted via HTTP requests with non form-specific >content-types in the header. In prior versions, data posted with content-types other than >multipart/form-data or application/x-www-form-urlencoded would still end up represented in >the request.POST attribute. Developers wishing to access the raw POST data for these cases, >should use the request.body attribute instead.

HTTP请求中的非表单数据request.POST将不再包含通过HTTP请求发布的数据,并且标头中包含非特定于表单的> content-types。在以前的版本中,使用除> multipart / form-data或application / x-www-form-urlencoded之外的内容类型发布的数据仍将最终在request.POST属性中表示。希望访问这些案例的原始POST数据的开发人员,>应该使用request.body属性。

However, given my data is wrapped in <form></form> elements in my template and generated using django's Form class, I can't understand why the data isn't in POST? How should I extract my form data?

但是,鉴于我的数据包含在我的模板中的

元素中并使用django的Form类生成,我无法理解为什么数据不在POST中?我该如何提取表单数据?

1 个解决方案

#1


0  

It seems you need to add application/x-www-form-urlencoded or multipart/form-data as enctype attribute to your form tag.

您似乎需要将application / x-www-form-urlencoded或multipart / form-data作为enctype属性添加到表单标记中。

#1


0  

It seems you need to add application/x-www-form-urlencoded or multipart/form-data as enctype attribute to your form tag.

您似乎需要将application / x-www-form-urlencoded或multipart / form-data作为enctype属性添加到表单标记中。