Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango form is not valid
    text
    copied!<p>I am trying to submit a form and it will not pass as <code>is_valid</code> in the view.</p> <p>The form uses forms.Modelforms which I do not have a lot of experience with.</p> <p>I checked <code>{{ form.errors }}</code> in the template but didn't get anything back.</p> <p>I appreciate the time and expertise</p> <p><strong>Form</strong></p> <pre><code>class AddSubcategory(forms.ModelForm): class Meta: model = Subcategory fields = ['category'] subcategory_name = forms.CharField(max_length=255) </code></pre> <p><strong>View</strong></p> <pre><code>@login_required @locationed def add_subcategory(request, location): subcat_form = AddSubcategory(request.POST) if subcat_form.is_valid(): submitted_subcat_name = subcat_form.cleaned_data['subcategory_name'] selected_cat = subcat_form.cleaned_data['category'] _, was_created = Subcategory.objects.get_or_create(name=submitted_subcat_name, category=selected_cat) return HttpResponseRedirect(reverse('manage_cat_subcat', args=(location.slug,))) else: cat_form = AddCategory() subcat_form = AddSubcategory() return render(request, 'inventory/manage_cat_subcat.html', {'location': location,'cat_form': cat_form,'subcat_form':subcat_form}) </code></pre> <p><strong>Template (form)</strong></p> <pre><code>&lt;form class="form-inline" action="{% url 'add_subcategory' location.slug %}" method="post"&gt; {% csrf_token %} {{subcat_form.category}} {{subcat_form.subcategory_name}} &lt;button class="btn btn-small" type="submit"&gt;Add Subcategory&lt;/button&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload