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