login Page by using django forms

前端 未结 2 919
陌清茗
陌清茗 2021-02-04 02:46

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)

相关标签:
2条回答
  • 2021-02-04 03:03

    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 %}
    
    0 讨论(0)
  • 2021-02-04 03:27

    Yes, you can. Actually, you don't need to create your own form. Simply use auth module and create your own login template. Read this: http://docs.djangoproject.com/en/dev/topics/auth/

    0 讨论(0)
提交回复
热议问题