login Page by using django forms

Nikhil Verma picture Nikhil Verma · Jan 13, 2011 · Viewed 27.5k times · Source

I am New beginner in python and django... i want to know how can i create a login form by using django forms(forms.py)

Answer

Marcus Whybrow picture Marcus Whybrow · Jan 13, 2011

In your urls.py file link to the built in Django login view, and pass in the path to a template you wish to use as the login page:

(r'^login/$', 'django.contrib.auth.views.login', {
    'template_name': 'myapp/login.html'
}),

And here is an example of what the template my look like (from the Django docs):

{% extends "mybase.html" %}

{% block content %}

    {% if form.errors %}
        <p>Your username and password didn't match. Please try again.</p>
    {% endif %}

    <form method="post" action="{% url 'django.contrib.auth.views.login' %}">
        {% csrf_token %}
        <table>
            <tr>
                <td>{{ form.username.label_tag }}</td>
                <td>{{ form.username }}</td>
            </tr>
            <tr>
                <td>{{ form.password.label_tag }}</td>
                <td>{{ form.password }}</td>
            </tr>
       </table>

       <input type="submit" value="login" />
       <input type="hidden" name="next" value="{{ next }}" />
   </form>

{% endblock %}