4

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 code -

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

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 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 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.

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.

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?

  • 1
    Did you try the last line of the release notes that you quoted? – Ngenator Apr 02 '13 at 14:33
  • yes, the request.body was also empty. Also, the documentation says "Non-form data in request.POST", but the input data is clearly a form-data, so it shouldn't be excluded in the first place, right? – Yu-Kuan Lin Apr 23 '13 at 08:19
  • Try email = form.cleaned_data.get('email') and password = form.cleaned_data.get('password') – nnmware May 30 '13 at 02:47

1 Answers1

0

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