login Page by using django forms

北慕城南 提交于 2019-12-03 09:28:53

问题


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)


回答1:


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 %}



回答2:


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/



来源:https://stackoverflow.com/questions/4679240/login-page-by-using-django-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!