简体   繁体   中英

Django login form error styling

I am attempting to create a custom login page for users in django. Here is the template I placed under registration/login.html

{% block content %}
<div class="container login-container">
    <h2>Login</h2>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="btn btn-default">Login</button>
    </form>
    <br>
    <p><strong>-- OR --</strong></p>
    ....
</div>
{% endblock %}

Here is how the page is reached" server/urls.py :

from django.contrib.auth import views as auth_views
...
    url(r'^login/$', auth_views.login, name='login'),
...

My issue is that I have no control over the way the errors or form objects are displayed and can find nothing in the documentation. If you know how I can control how the form is rendered that would be much appreciated.

Thank you!

I solved it. Here is the code I used for the solution and a link to the documentation

Custom form rendering

<h2>Login</h2>
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post">
    {% csrf_token %}
    <p>{{ form.username.label_tag }} {{ form.username }}</p>
    <p>{{ form.password.label_tag }} {{ form.password }}</p>
    <button type="submit" class="btn btn-default">Login</button>
</form>

Detailed docs: https://docs.djangoproject.com/en/1.11/topics/auth/default/#all-authentication-views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM