django accessing errors - django

I have the following:
{% if formDetails.errors %}
{% for key, value in formDetails.errors.items %}
{% for error in value %}
<div class="ui-widget" id="id-error">
<div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
<strong>{{ key }}:</strong> {{ error }}</p>
</div>
</div>
{% endfor %}
{% endfor %}
But unfortunately key gives me the name of the model field (the one that's lowercase with underscores). How can I get the nice looking name like field.label_tag?

The form.errors template variable is a list not a dictionary, so you should access it as follows:
{% for error in form.errors %}
<li>{{ error }}</li>
{% endfor %}
In addition it contains form wide errors raised by the clean() method on the Form object you are dealing with. Validation errors raised by the individual fields, can be accessed like this:
{% for field in form %}
{% for error in field.errors %}
{{ error }}
{% endfor %}
{{ field.label_tag }}
{% endfor %}
Have a read of this part of the Django docs, it seems like you don't have a full understanding of what you have when accessing a form.

Related

How to show the user the error when submitting a Django form?

I have created a Django form that creates a user through a custom user model, but if the form is invalid, the form disappears and leaves the button on the screen. I am trying to display the errors through field.errors.
<form method="post">
{% csrf_token %}
{% for field in registration_form %}
<h5>{{ field.label_tag }}
{{ field }}
{% if field.help_text %}
<span>{{ field.help_text }}</span>
{% endif %}
{% for error in field.errors %}
<p>{{ error }}</p>
{% endfor %}
</h5>
{% endfor %}
<button type="submit">Join</button>
</form>
Does anyone know what is wrong with the error part? (The form works; it just does not show any errors if it is not vallid.)
You need to do it like this:
{% if form.errors %}
{{ form.errors }}
{% endif %}

How to keep 2 fields using django forms

As django forms run a loop for the fields
{% for field in form %}
<div class="col-6">
{{ field.errors }}
{{ field.label_tag }}
{{ field }}
</div>
{% endfor %}
I actually want to have 2 fields, I found a cheap solution to this issue using Bootstrap4
I created the columns by using col-6 by which I get 2 fields in the row. But what if I want to make custom designs of forms in django ?
Personally i am using this method to show only fields i want and then have if statements for the way each field should be shown
{% for i in form %}
{% if i.name in 'title,address,city' (Fields you want to show) %}
{% for error in i.errors %}
<div>
<li><strong>{{ error|escape }}</strong></li>
</div>
{% endfor %}
<p>
<label>{{i.label}}:</label>
<input type="text" name="{{i.name}}" required id="id_{{i.name}}">
</p>
{% endif %}
{% endfor %}
And like that you can style it however you want and show only those you want to show

Django form template

If there were field.errors in django, how to to add css class to {{ field }}, example at bottom
{% for field in form %}
{{ field }}
{% if field.errors %}{{ field.errors }}{% endif %}
{% endfor %}
Aiming for {{ field }} input type that would have "class=error" when ever there was an error
<input class="error">
You can add the class to a wrapper around the field. Django also does this by itself when defining classes in forms.py or views.py
{% for field in form %}
<p {% if field.errors %}class="error"{% endif %}
{{ field }}
</p>
{% if field.errors %}{{ field.errors }}{% endif %}
{% endfor %}
Unfortunately this seems to be the only way.
Even when Django adds the classes itselt, it prefers to add them to a wrapper of some sort.
http://docs.djangoproject.com/en/dev/ref/forms/api/#more-granular-output
Scroll down to read about the new thing in 1.2 about adding css classes.
Using this, you can just use form.as_p (or whatever suits you) as the elements will have all the classes as you define them.
Hope this was a little bit of help.
You can use widget_tweaks api to do that.
Instead of rendering a form with
{{ form.field }}
You should use:
{% load widget_tweaks %}
{% render_field form.field class="error" %}

Django, loop over all form errors

At my template, I want to iterate through all form errors, including the ones that are NOT belonging to a specific field. ( which means for form.errors, it should also display for __all__ errors aswell)
I have tried several versions, Ie:
<div id="msg">
{% if form.errors %}
<div class="error">
<p><span>ERROR</span></p>
<ul>
{% for key,value in form.errors %}
{% for error in value %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
</div>
Still no achievement, I will be greatful for any suggestion.
Form errors in Django are implemented as an ErrorDict instance (which is just a subclass of dict with extras). Try a slight adjustment to your template for loop syntax:
{% for key, value in form.errors.items %}
Are you, by any chance, looking for form.non_field_errors? That is how you would get access to the errors that aren't associated with a particular field.
{% if form.non_field_errors %}
<ul>
{{ form.non_field_errors.as_ul }}
</ul>
{% endif %}
Check the forms.py test suite as well for another example. Search for form.non_field_errors

Customizing a Django admin template

I would like to modify an admin template in Django.
% cat /Library/Python/2.5/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html
<fieldset class="module aligned {{ fieldset.classes }}">
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}<div class="description">{{ fieldset.description|safe }}</div>{% endif %}
{% for line in fieldset %}
<div class="form-row{% if line.errors %} errors{% endif %} {% for field in line %}{{ field.field.name }} {% endfor %} ">
{{ line.errors }}
{% for field in line %}
<div{% if not line.fields|length_is:"1" %} class="field-box"{% endif %}>
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{{ field.label_tag }}{{ field.field }}
{% endif %}
{% if field.field.field.help_text %}<p class="help">{{ field.field.field.help_text|safe }}</p>{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
</fieldset>
What kind of object is field, and more specifically how would I get the name of a field?
field is an instance of AdminField and field.field is an instance of BoundField, so you can reference the fields name with:
{{ field.field.name }}
Once as you start to dive deep into admin customisation, its the only place the documentation is really lacking. that being said, the code is well written and easy to understand if you take the time to research it, IMHO.
There is not many files so take an evening and read through them. In your case, I would start with:
contrib/admin/sites.py
contrib/admin/options.py
contrib/admin/helpers.py
Have you done your research?
After that, I would start poring over the python code that invokes your template. I would imagine that field is from the forms system
Field
A class that is responsible for doing validation, e.g. an EmailField
that makes sure its data is a valid
e-mail address.