django modelformset_factory - management form data is missing - django

I am still fighting with formsets and I cant really understand why I am getting this error:
u'ManagementForm data is missing or has been tampered with
Thats my code:
Please point out my mistakes and help me with resolving this issue.
#csrf_protect
#transaction.commit_on_success
def signup(request):
form = NewUserCreationForm()
doc_form = NewDocRegisterForm()
SpecialityLicensesFormSet = modelformset_factory(SpecialityLicenses, extra=1, exclude = ('user'))
formset = SpecialityLicensesFormSet(queryset=SpecialityLicenses.objects.none())
if request.method == "POST":
form = NewUserCreationForm(request.POST or None)
doc_form = NewDocRegisterForm(request.POST or None)
formset = SpecialityLicensesFormSet(request.POST or None)
if form.is_valid() and doc_form.is_valid() and formset.is_valid():
user = form.save()
doc = doc_form.save(commit=False)
doc.user = user
doc.save()
print formset
fset = formset.save(commit=False)
for n in fset:
n.user = user
n.save()
return HttpResponse("Uzytkownik utowrzony")
return render_to_response("userena/signup_new.html", {'form': form,
'doc_form': doc_form,
'spec_form': formset,},
context_instance=RequestContex
t(request))
Template code:
<form action="/en/accounts/doc_register/" method="post">{% csrf_token %}
{% for field in form %}
<div>
{% if field.errors %}
{{ field.errors|striptags }} |
{% endif %}
{{field.label}} | {{ field}}
</div>
{% endfor %}
<hr>
{% for f in doc_form %}
<div>
{% if f.errors %}
{{f.errors|striptags}} |
{% endif %}
{{f.label}} : {{ f }}
</div>
{% endfor %}
<hr>
{{ spec_form.management_form }}
{{ spec_form }}
<hr>
<input type="submit" value="Submit"/>
</form>
{% endblock %}

you don't need to do {{ formset.management_form }} if you do {{ formset }}, just if you do
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
try removing the {{ spec_form.management_form }} bit from your template. Look at the third example

add prefix for formset if prefix is missing for formset it will give error

Related

Why this django formset has stopped saving the content all of a sudden?

I had this view that rendered a form and a formset in the same template:
class LearnerUpdateView(LearnerProfileMixin, UpdateView):
model = User
form_class = UserForm
formset_class = LearnerFormSet
template_name = "formset_edit_learner.html"
success_url = reverse_lazy('pages:home')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
learner = User.objects.get(learner=self.request.user.learner)
formset = LearnerFormSet(instance=learner)
context["learner_formset"] = formset
return context
def get_object(self, queryset=None):
user = self.request.user
return user
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
user = User.objects.get(learner=self.get_object().learner)
formsets = LearnerFormSet(self.request.POST, request.FILES, instance=user)
if form.is_valid():
for fs in formsets:
if fs.is_valid():
# Messages test start
messages.success(request, "Profile updated successfully!")
# Messages test end
fs.save()
else:
messages.error(request, "It didn't save!")
return self.form_valid(form)
return self.form_invalid(form)
Then i wanted to make it prettier and i added the select2 multicheckbox widget and the django-bootstrap-datepicker-plus
Nothing has changed elsewhere, yet when i submit the post it only saves the data relative to User and not to Learner (which relies on the formset)
According to the messages, the formset data is not validated, I don't understand why since i didn't touch the substance at all but just the appearance.
Being a beginner im probably missing something big, I thank in advance whoever can help me find out the problem.
Here below the forms and the template:
(users.forms)
class LearnerForm(forms.ModelForm):
class Meta:
model = Learner
fields = ['locations', 'organization', 'supervisor', 'intro']
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'birthday', 'email', 'profile_pic']
widgets = {
'birthday': DatePickerInput(format='%Y-%m-%d'), }
LearnerFormSet = inlineformset_factory(User, Learner, form=LearnerForm)
template
{% extends '_base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
{{ learner_formset.management_form}}
{% for form in learner_formset %}
{% if forloop.first %}
{% for field in form.visible_fields %}
{% if field.name != 'DELETE' %}
<label for="id_{{ field.name }}">{{ field.label|capfirst }}</label>
<div id='id_{{ field.name }}' class="form-group">
{{ field.errors.as_ul }}
{{ field }}
</div>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
<input class="btn btn-success" type="submit" value="Update"/>
</form>
{% endblock content %}
I figured it out by combing the previous commits. The problem was in the template, the formset wasn't getting validated because i had ignored the hidden fields, which i hear are a common problem when dealing with formsets.
So the correct code is as such:
{% extends '_base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
{{ learner_formset.management_form}}
{% for form in learner_formset %}
{% if forloop.first %}
{% comment %} This makes it so that it doesnt show the annoying DELETE checkbox {% endcomment %}
{% for field in form.visible_fields %}
{% if field.name != 'DELETE' %}
<label for="{{ field.name }}">{{ field.label|capfirst }}</label>
<div id="{{ field.name }}" class="form-group">
{{ field }}
{{ field.errors.as_ul }}
</div>
{% endif %}
{% endfor %}
{% endif %}
{% for field in form.visible_fields %}
{% if field.name == 'DELETE' %}
{{ field.as_hidden }}
{% else %}
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
<input class="btn btn-success" type="submit" value="Update"/>
</form>
{% endblock content %}

Inline form validation returns empty formset.errors list

I have the following inline formset:
CreateClientFormset = inlineformset_factory(
Client,
EventType,
fields=(
'name',
),
extra=0,
can_delete=True,
min_num=1,
validate_min=True,
widgets={
'name': forms.TextInput(attrs={
'class':'form-control',
'maxlength' : 50
})
})
And the following view:
def create_client(request):
form = CreateClient()
formset = CreateClientFormset(instance=Client())
if request.method == 'POST':
form = CreateClient(request.POST)
if form.is_valid():
client_saved = form.save(commit=False)
client_formset = CreateClientFormset(request.POST, request.FILES, instance=client_saved)
if client_formset.is_valid():
client_saved.save()
client_formset.save()
return redirect(clients)
else:
print client_formset.errors
else:
print form.errors
return render(request, 'create_client.html', {'form' : form, 'formset' : formset})
When I try to submit an empty value to the formset, it prints the error to the console but it doesn't do anything in the template.
This is my template:
{% for field in formset %}
{{ formset.management_form }}
<tr>
<td><label>{{ field.name.label }}</label></td>
<td>
{% if field.instance.pk %}{{ field.DELETE }}{% endif %}
{{ field.name }}
{{ formset.errors }}
{{ field.id }}
{% for hidden in formset.hidden_fields %}
{{ hidden }}
{% endfor %}
</td>
</tr>
{% endfor %}
{{ formset.errors }} displays [], even before the form submission.
What am I doing wrong? Thanks.
I solved it like this:
if client_formset.is_valid():
client_saved.save()
client_formset.save()
return redirect(clients)
else:
print client_formset.errors
return render(request, 'create_client.html', {'form' : form, 'formset' : formset, 'formset_errors' : client_formset.errors})
And in the template:
{% for field in formset %}
<tr>
<td><label>{{ field.name.label }}</label></td>
<td>
{% if field.instance.pk %}{{ field.DELETE }}{% endif %}
{{ field.name }}
{% if formset_errors %}
{% for errors in formset_errors %}
{% for error in errors.values %}
{{ error }}
{% endfor %}
{% endfor %}
{% endif %}
{{ field.id }}
{% for hidden in formset.hidden_fields %}
{{ hidden }}
{% endfor %}
</td>
</tr>
{% endfor %}
You render formset:
return render(request, 'create_client.html', {'form' : form, 'formset' : formset})
but the one loaded with your form data/errors is client_formset:
print client_formset.errors
The second one you need to pass to render since you are using {{ formset.errors }} in your template.

Django - Add 3 users with one form

I'm trying to create a form that save 3 users.
my forms:
class UserForm(forms.ModelForm):
class Meta:
model = MyUsers
exclude = ('address',)
my views:
def adduser(request):
if request.method == "POST":
rform = UserForm(request.POST, instance=MyUsers())
if rform.is_valid():
new_users = rform.save()
return HttpResponseRedirect...
else:
rform = UserForm(instance=MyUsers())
return render_to_response...
my template structure:
<form method="post">
{% for field in rform %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% endfor %}
{% for field in rform %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% endfor %}
{% for field in rform %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% endfor %}
<input type="submit" value="Save" />
</form>
the problem
The form works properly but add only the last inserted user.
Alasdair's suggestion (formset) is a way to generate multiple instances of the same form at once.
The forms can be rendered in the template by looping over the formset instance:
{% for form in formset %}
{{ form.id }} #PK field is MANDATORY, see reference[2] below
{{ form.field1 }}
{{ form.field2 }} #or just as something {{ form.as_p }}
{% endfor %}
In your views, formset validation is made once for all if formset.is_valid(): and rformset = formset_factory(MyUsers, extra=1, max_num=3) to create the formset.
Don't forget the imports.
References, both from Django Docs:
Formsets {1}
Creating forms from models {2}

django form error messages not showing

So I have my view:
def home_page(request):
form = UsersForm()
if request.method == "POST":
form = UsersForm(request.POST)
if form.is_valid():
form.save()
c = {}
c.update(csrf(request))
c.update({'form':form})
return render_to_response('home_page.html', c)
my forms.py:
class UsersForm(forms.ModelForm):
class Meta:
model = Users
widgets = {'password':forms.PasswordInput()}
def __init__(self, *args, **kwargs):
super( UsersForm, self ).__init__(*args, **kwargs)
self.fields[ 'first_name' ].widget.attrs[ 'placeholder' ]="First Name"
self.fields[ 'last_name' ].widget.attrs[ 'placeholder' ]="Last Name"
self.fields[ 'password' ].widget.attrs[ 'placeholder' ]="Password"
and my template:
<html>
<body>
<form method="post" action="">{% csrf_token %}
{{ form.first_name }} {{form.last_name }} <br>
{{ form.password }} <br>
<input type="submit" value="Submit Form"/>
</form>
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<p> {{ errors }} </p>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<p> {{ error }} </p>
{% endfor %}
{% endif %}
</body>
</html>
Now, keep in mind that before I split the form field, my form just looked like this:
<form method="post" action="">{% csrf_token %}
{{ form }}
<input type="submit" value="Submit Form"/>
</form>
and if the form had a problem (like if one of the fields was missing) it would automatically give an error message. After I split up the form fields in the template (made {{ form.first_name }}, {{ form.last_name }} and {{ form.password }} their own section) it stopped automatically giving error messages. Is this normal?
But the main problem is, how come my {{ if form.errors }} statement is not working / displaying error messages if there are error messages? For example, if I purposely not fill out a field in the form and I click submit, the database does not get updates (which is a good thing) but it does not give any error messages. Any idea why?
I also tried remove the {{ forms.non_field_errors }} and tried to return just field errors like so:
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<p> {{ errors }} </p>
{% endfor %}
{% endfor %}
{% endif %}
but it still doesn't work.
I found the mistake (typo).
The snippet should be:
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<p> {{ error }} </p>
{% endfor %}
{% endfor %}
{% endif %}
I had errors instead of error.
you have to send your form context again if is_valid() isn't true,
if form.is_valid():
return redirect('/user/contact/')
else:
return render(request, 'users/contact.html', context={'form': form})
because your errors will display in your form
else: it will return ValueError
Add this snippets in views.py like this
if request.method == "POST":
fm = UserRegsitrationForm(request.POST)
if fm.is_valid():
fm.save()
else:
fm = UserRegsitrationForm()
context = {'fm':fm}
return render(request,'registration.html',context)
The error capture could very well have been done with:
{% for field in form %}
{{ field.errors }}
{% endfor %}

django - form has no errors but form.is_valid() doesn't validate

I have a form that for some reason it it doesn't throw any errors (if user adds data in the text field) but form.is_valid() doesn't validate. Any ideas?
forms.py:
class MyForm(forms.Form):
title = forms.CharField(widget=forms.TextInput(attrs={'class':'titleField'))
mytemplate.html
<form action="" method="post" name="form">{% csrf_token %}
{{ form.title.errors }}
{{ form.title }}
<input type="submit" name='submit_button' value="Post" />
</form>
views.py:
if 'submit_button' in request.POST:
form = MyForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
title = cd['title']
g = MyData(title='title')
g.save()
else:
form = MyForm()
From your template, add the following:
{{ form.errors }}
{{ form.non_field_errors }}
Do you see any errors from the above?
You can also print (or log) error at back end too, Similarly use this:
Like, to print error:
print(form.errors)
I'm using this block with bootstrap
{% if form.errors %}
<div class="alert alert-danger" role="alert">
{% for field, errors in form.errors.items %}
{% for error in errors %}
<b>{{ field }}</b>: {{ error }}
{% endfor %}
{% endfor %}
</div>
{% endif %}