Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

login Page by using django forms

Tags:

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)

like image 322
Nikhil Verma Avatar asked Jan 13 '11 10:01

Nikhil Verma


People also ask

How do I create a login form in Django?

Django by default will look within a templates folder called registration for auth templates. The login template is called login. html . Create a new directory called templates and within it another directory called registration .


1 Answers

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 %}
like image 154
Marcus Whybrow Avatar answered Oct 21 '22 03:10

Marcus Whybrow