Search code examples
pythondjangoformsdjango-registration

Django Editing Native Register Form


In register.html template if I put

{{ form.as_p }}

to use native register form it works. But it is very ugly. So I use it as

{{ form.username}}
{{ form.password1}}

But sometimes when an error occurs, I need to check if register form has any errors [example names]

{% if form.any_error %}

And show the message styled

{{ form.errormessage}} or {{ form.errorlist.values}}

How to achive this in Django with native form and what are the native form variables let us to do this?


Solution

  • I would suggest to take a look at Widget Tweaks You can add classes and attributes to your Form fields which gives you a lot of control over the Form styling.

    <form method='POST' action='' enctype='multipart/form-data'>
        {% load widget_tweaks %}
        {% csrf_token %}
    <span>1. Title: {{ form.title|add_class:"titleCSS" }}</span>
    
    {% if form.errors %}
                <span> {{ form.errors|add_class:"errorCSS" }} </span>
    {% endif %}
    </form>
    

    Or As mentioned before you can use Crispy Forms they work fine and a lot of sites are using it but you have to work into the Crispy Forms package. With Widget Tweaks you can simply add CSS as you would do it in HTML.