开发者

Django: Form GET has "field required" errors while POST doesn't

开发者 https://www.devze.com 2023-03-07 00:11 出处:网络
Coding in Django, using forms. I\'m having a problem where if the form is GET, then every field always shows up with an error \"This field is required.\", but the problem doesn\'t exist if the form is

Coding in Django, using forms. I'm having a problem where if the form is GET, then every field always shows up with an error "This field is required.", but the problem doesn't exist if the form is POST. Here's a simple example (partial code),

views.py:

def login(request):        
    if request.method == 'GET':
        form = RegisterForm (request.GET)
    else:
        form = RegisterForm ()
return render_to_response('register.html', locals(), context_instance=RequestContext(request))

register.html:

<form action = "" method = "GET">
    <table style = "border: none;">
        <tr>
            <td class = "login"><strong>email:</strong></td>
            <td class = "login">{{ form.email }}</td>
            <td><span class = "error">{{ form.errors.email }}</span></td>
        </tr>

        ...
    </table>
</form>

If you change all the 'GET' to 'POST' everything's fine. Or else the 'form.errors.email' will always throw a "This field is required." error.

Another odd thing... doesn't seem like I see any form objects being initiated with request.GET. The Django Book 2.0 only shows the form object initiated with request.POST. Is there something I'm missing here?

Many thanks for any tips.

EDIT: thanks for the tips from Craig and Ignacio. However, my question is that regardless of what I'm doing with this form, either GET or POST, having GET in the form always gives me form errors about "field required". That's the mechanic I'm not unde开发者_高级运维rstanding. Any help on that would be greatly appreciated, thanks.


If I'm not mistaken, the default HTTP request method is always GET unless it is specified as POST. So when you access that view (using GET by default), you are already satisfying the if request.method == 'GET': statement, so it is automatically bringing up the validation error for inserting an empty data into the field. You never actually get to the else: form = RegisterForm() lines

Check out django-debug-toolbar which is a very helpful toolbar and it allows you to see you request type and requet variables.


First if you're using a login, you should always use post when logging the user in.

Second, you're initializing an empty form in one place, but not in the other area. What you likely will want is:

    def login(request):        
        if request.method == 'POST':
             form = RegisterForm (request.POST)
             # Some logic to validate the user
        else:
             form = RegisterForm ()
             return render_to_response('register.html', locals(), context_instance=RequestContext(request))

Your action value isn't required if you want to post to the same page. Also if you're using a POST (as you should), then you should be using a {% csrf_token %} within your form.


Omit the action attribute. By default, submission is to the same URL (unless using GET, of course).

0

精彩评论

暂无评论...
验证码 换一张
取 消