Reputation: 1462
I have a Django form with one required field (and many that aren't). The problem is, the form acts as though the page has already been submitted without the required field when it loads. The required field is red and has a reminder that the field is required, when it should just have a red asterisk next to the field if it's required and there haven't been any attempts to submit it while blank yet.
Here is my class declaration from views.py:
class UpdateItem(UpdateView):
model = Item
form_class = ItemForm
form_class.required_css_class = 'required'
form_class.error_css_class = 'error'
template_name = "location/inventory/add_item.html"
I can't find anywhere in the page itself's jQuery where it would submit itself on loading, and other classes have a similar declaration but work properly, so I don't think it's related to that. Looking at the source of the generated page, I get:
<tr class="required error"><th><label for="id_model">Model:</label></th>
<td><ul class="errorlist"><li>This field is required.</li></ul>
when I should be getting
<tr class="required"><th><label for="id_model">Model:</label></th>
Is it likely to be some accidental submission of the form that I haven't found yet, or could it be something else?
Upvotes: 2
Views: 1210
Reputation: 1462
The problem turned out to be that elsewhere in views.py, the POST data was being copied for validation without checking to see if there was any POST data yet. Solved by checking to see if request.method == "POST"
.
Upvotes: 3