django form error messages not showing - django

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 %}

Related

My form does not save using bootstrap for styling in django

hello thank you for your visiting
I'm a new learner of Django
I would like to know how to style form using django-bootstrap-v5
i try this and does not work
i have this form.py
pathesForm = inlineformset_factory(
Book,
quotation,
fields=('name','time',),
can_delete=False,extra=4,max_num=4,
widgets={'name': forms.TextInput(attrs={
'placeholder': 'name of book',
})
}
)
i use django-bootstrap-v5 and in file html
this form is not working with me
<form role="form" class="form-horizontal" method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
<div class="row">
<div class="col-md-6">
{% bootstrap_field form.name %}
</div>
<div class="col-md-6">
{% bootstrap_field form.time %}
</div>
</div>
{% if form.maybemissing %}
{% bootstrap_field form.maybemissing %}
{% endif %}
{% endfor %}
<button type="submit">Save</button>
</form>
but this is working with me (i can save the form)
{% bootstrap_formset_errors formset %}
<form role="form" class="form-horizontal" method="post">
{% csrf_token %}
{% bootstrap_formset formset %}
<button type="submit">Save</button>
</form>
this is my view.py
def hello(request,id):
book=Book.objects.get(id=id)
if request.method == 'POST':
form= pathesForm(request.POST,request.FILES,instance=book)
if form.is_valid():
form.save()
form = pathesForm(instance=book )
return render(request,'hello/pathesForm.html',{'formset':form})
i use print('hello) to try know where is the problem and the result seems like the form is not valid
how i can to customize the style of my form like the first one
in your views you forgot to pass then else statement so here it is
def hello(request,id):
book=Book.objects.get(id=id)
if request.method == 'POST':
form= pathesForm(request.POST,request.FILES,instance=book)
if form.is_valid():
form.save()
else:
form = pathesForm(instance=book)
return render(request,'hello/pathesForm.html',{'formset':form})
and in your html while requsting the file field
you have to give form a encryptio type here it is
<form role="form" class="form-horizontal" method="POST" enctype="multipart/form-data">
this has to solve your problem and tell me if you still getting any error have a good day
allways if you have a problem with from use print(form.errors) before the form is valid and after it
i got my problem and i got this error [{'id': ['This field is required.']}
soulition:
<form role="form" method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="row">
<div class="col-md-6">
{% bootstrap_field form.name %}
</div>
</div>
{% if form.maybemissing %}
{% bootstrap_field form.maybemissing %}
{% endif %}
{% endfor %}
i think it success using formset only because of ( id add automatically )
and if you want to forloop you should be add it manually

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 %}

Flask/Jinja2 form submit button not working

I've got a simple form where a user enters two dates. My input is getting passed correctly, but the Submit button isn't working. Here's my view:
# GLASS ------------------------------------------------------------------------
class NameForm(Form):
starts_on = StringField('Starts', validators=[Required()])
ends_on = StringField('Ends', validators=[Required()])
submit = SubmitField('Go')
#app.route('/glass/', methods = ['GET', 'POST'])
def glasses():
starts_on = None
ends_on = None
results = None
form = NameForm()
if form.validate_on_submit():
starts_on = form.starts_on.data
ends_on = form.ends_on.data
# SQL takes starts_on, and ends_on as inputs
results, start_date, end_date, companies_tracked = diagUserActs.userTime(starts_on, ends_on, diagUserActs.companies)
form.starts_on.data = ''
form.ends_on.data = ''
return render_template('glass.html', form = form, results = results)
And here's my template:
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
<!--{% block title %}Glasses{% endblock %}-->
{% block content %}
<form class="form-inline" method="post" role="form">
{{ wtf.form_field(form.starts_on) }}
{{ wtf.form_field(form.ends_on) }}
{{ wtf.form_field(form.submit) }}
</form>
Oddly enough, this method below works, but I'd like to list the form elements individually so I've got more control regarding its presentation:
<form class="form-inline" method="post" role="form">
{% for field in form %}
{{ field.label }}
{{ field(placeholder="YYYY-MM-DD") }}
{% endfor %}
</form>
What I've done before is use something like this:
{% macro render_field(field, class='None') %}
<div class="pure-control-group">
{{ field.label(class_=class)|safe }}
{{ field(class_=class)|safe }}
{% if field.errors %}
<p class="errors">
{% for error in field.errors %}
<p>
{{ error }}
</p>
{% endfor %}
</p>
{% endif %}
</div>
{% endmacro %}
Then use it something like this:
{{ render_field(form.username) }}
Where form is something like this:
form = forms.Login(request.form)
It seems there is something wrong with the form here:
{{ wtf.form_field(form.starts_on) }}
I've never seen it used that way.

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

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 %}