Every form validation in formset - django

Some troubles with validation.
I'm using this construction in my template:
<form method="POST">
{% csrf_token %}
{{ formset.media.js }}
{% for form in formset %}
<p>{{ form }}</p>
{% endfor %}
<button type="submit" class="btn btn-primary">Send</button>
And this validation in views:
def flow_type(request):
patternFormset = modelformset_factory(CashFlowPattern, fields='__all__')
if request.method == 'POST':
formset = patternFormset(request.POST)
if formset.is_valid():
formset.save()
formset = patternFormset()
template = 'cash_table/index.html'
context = {
# 'form': form,
'formset': formset
}
return render(request, template, context)
I get form at page but nothing happens after submit.
But if I use another template construction it works:
<form method="POST">
{% csrf_token %}
{{ formset.media.js }}
{{ formset }}
<button type="submit" class="btn btn-primary">Send</button>
</form>
But then I get all fields of new form at the same line.

I think you need to have {{ formset.management_form }} in the template where you iterate through the forms of the formset so that Django knows that it is a formset, and knows how many forms are in the formset, etc...
<form method="POST">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.media.js }}
{% for form in formset %}
<p>{{ form }}</p>
{% endfor %}
<button type="submit" class="btn btn-primary">Send</button>
When you use {{ formset }} the management is done automatically (it's a shortcut).
Source: https://docs.djangoproject.com/en/4.1/topics/forms/formsets/#custom-formset-validation

Related

Unable to see error messages in registration template. Django

Good afternoon Community,
I hope you are all well.
I am building my first website and I have the following problem; when I try to create a new user, if the user enters the wrong data the template does not show the error. Any hints?
Template:
<form action= "" method='post'>
{% csrf_token %}
{% for field in form %}
<p>{{field.label}}{{field}}</p>
{% endfor %}
<button type="submit" class="btn btn-success">Create User</button>
Views.py:
def register_page(request):
form = UserForm
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('http://127.0.0.1:8000/login_user/')
context = {'form' : form}
return render(request, 'simple_investing/register.htm', context)
Forms.py:
class UserForm(UserCreationForm):
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
As is discussed in the rendering fields manually section of the documentation, for the fields you should also render {{ field.errors }}, and the {{ form.non_field_errors }} which deals with errors not specific to one form.
The template thus should look like:
<form action= "" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
{% for field in form %}
<p>
{{ field.errors }}
{{ field.label }}
{{ field }}
</p>
{% endfor %}
<button type="submit" class="btn btn-success">Create User</button>
</form>
The section also discusses how to enumerate over the errors, and apply certain styling to these errors.

My django form is not working when i iterate through it using for loop

Basically if I tried to use this code
{% for field in form %}
<div class="input">
<label for="" class="labelinput">{{field.label}}</label>
{{field}}
</div>
{% endfor %}
the form data wont make it pass is_valid().But it renders out the form fine. and if I use this code
<form action="" method="post"> {% csrf_token %}
{{form}}
<input type="submit" value="">
it worked perfectly fine. How do I get the first code to work because I want to add classes between the label and the input field
and here's my view
def booklist_view(request):
bkff = BookListForm()
if request.method == 'POST':
bkff = BookListForm(request.POST)
if bkff.is_valid():
bkff.save()
bkff = BookListForm()
context = {
'form': bkff,
}
return render(request, 'booklist1st/booklist.html', context)
Please try this.
views.py
def booklist_view(request):
form = BookListForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
form.save()
context = {'form': form }
return render(request, 'booklist1st/booklist.html', context)
Here we render field according according to field type(hidden_fields,visible_fields).
html template:
<form method="post">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="input">
{{field.label}}
{{field}}
</div>
{% endif %}
<input class="btn btn-primary" type="submit" name="add_book" value="Save and add book" />
</form>
You need to specify each field relate data like for=, id=, etc. To have maximum control over how your form is rendered, specify each field and control its style, for example, as we can't see your Form definition, this is an example on how it would be for a title field:
<form method="post">{% csrf_token %}
{# Include the hidden fields #}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{# Include the visible fields #}
{{ form.non_field_errors }}
{{ form.title.errors }}
<label for="{{ form.title.id_for_label }}">Title</label>
{{ form.title }}
{% if form.title.help_text %}
<small id="titleHelp">{{ form.title.help_text|safe }}</small>
{% endif %}
</div>
<input class="btn btn-primary" type="submit" name="add_book" value="Save and add book" />
</form>
<form method="post">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="input">
<label for="" class="label">{{field.label}}</label>
{{field}}
</div>
{% endfor %}
<input class="btn btn-primary" type="submit" name="add_book" value="Save and add book" />
</form>
View.py
from django.views.decorators.csrf import csrf_protect
#csrf_protect
def booklist_view(request):
bkff = BookListForm()
if request.method == 'POST':
bkff = BookListForm(request.POST)
if bkff.is_valid():
bkff.save()
context = {
'form': bkff,
}
return render(request, 'booklist1st/booklist.html', context)

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}

How to display choicefield select widget on template?

This is my view:
CHOICES = (('10','10'), ('20','20'),('30','30'), ('50','50'))
class Droplist (forms.Form):
number = forms.ChoiceField(choices = CHOICES)
def page_objects(request):
if request.method == 'POST': # If the form has been submitted...
form = Droplist(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
pass #pages = form.cleaned_data['value']
#return AutoPaginateNode(paginate_by=pages) # Redirect after POST
else:
form = Droplist() # An unbound form
return render_to_response('pagination.html', {'form': form })
This is my template:
<form action="/submit/" method="post">{% csrf_token %}
{{ form }}
<input type="submit" value="Select">
</form>
how can i render my form, because i need to have a dropdown box with choices on template? what i've missed?
You need to use {{ form.as_p }}:
<form action="/submit/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Select">
</form>
You can also use {{ form.as_table }} to output table rows but you'll need to provide your own <table> and form.as_ul to output list items:
<form action="/submit/" method="post">{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Select">
</form>
You can find more information on Displaying a form using a template on Django's documentation

django modelformset_factory - management form data is missing

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