使用“django.contrib.auth.views.login”时出错

时间:2017-07-18 18:05:33

标签: django authentication django-login

我收到错误“在include()的情况下,视图必须是可调用的或列表/元组。”同时尝试使用django的内置登录系统(登录,注销,logout_then_login)。任何人都可以解决这个问题。

书签/帐户/ urls.py -

from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login', 
name='logout_then_login'),
]

书签/ urls.py -

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^account/',include("account.urls"))
]

模板/注册/ login.html的 -

<body>

<h1>Log-in</h1>
{% if form.errors %}
    <p>Your username and password didn't match. Please try again.</p>
{% else %}
    <p>Please, use the following form to log-in.
{% endif %}

<div class="login-form">
    <form action="{% url "login" %}" method="post">
        {{ form.as_p }}
        {% csrf_token %}
        <input type="hidden" name="next" value="{{ next }}" />
        <p><input type="submit" value="Log-in"></p>
    </form>

</body>

模板/注册/ logged_out.html -

<h1>Logged out</h1>
<p>You have been successfully logged out. You can <a href="{% url "login" 
%}">log-in again</a>.</p>

</body>

1 个答案:

答案 0 :(得分:1)

我假设你正在使用django&gt; 1.9:所以你不能再使用字符串作为视图了。所以你需要对所有观点做这样的事情:

from django.contrib.auth.views import login
#.....

    url(r'^login/$', login, name='login'),
    ...

还包括:

from django.contrib import admin
from accounts import urls

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^account/',include(urls))
]