让Django的@login_required装饰器工作还需要做些什么?

Chr*_*ard 11 python django django-authentication

我正在尝试使用Django的帐户系统,包括@login_required装饰器.我的settings.py文件包含django.contrib.auth,我已经完成了syncdb.

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/accounts/login/?next=/
Using the URLconf defined in dashboard.urls, Django tried these URL patterns, in this order:
^$ [name='home']
The current URL, accounts/login/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Run Code Online (Sandbox Code Playgroud)

在尝试@ login_required-decorate我的主视图后,我看到了上面的内容.

它似乎很窒息,因为它被重定向到accounts/login /,我还没有在urls.py中准备好.

我可以添加到urls.py或其他地方,以便login_required装饰器将执行其通常的行为?

谢谢,

key*_*ser 19

设置LOGIN_URL在你的设置.默认值为'/accounts/login/'

装饰器还带有一个可选login_url参数:

@login_required(login_url='/accounts/login/')
Run Code Online (Sandbox Code Playgroud)

并且,从文档:

请注意,如果未指定login_url参数,则需要确保settings.LOGIN_URL和登录视图正确关联.例如,使用默认值,将以下行添加到URLconf:

(r'^accounts/login/$', 'django.contrib.auth.views.login'),
Run Code Online (Sandbox Code Playgroud)

  • 实际上,有一个默认值:您可以使用管理员登录模板:(r'^ accounts/login/$','django.contrib.auth.views.login',{'template_name':'admin/login.html'} ,name ="my_login")请参阅:http://stackoverflow.com/questions/2867213/is-there-a-built-in-login-template-in-django (3认同)