Django中有内置的登录模板吗? [英] Is there a built-in login template in Django?
问题描述
我想让用户在看到页面之前登录。有没有内置的用户登录模板,这样我就不必自己写登录页面了。
I want to let a user sign in before seeing pages. Is there any built-in template for user sign in, so that I do not have to write my own sign in page?
推荐答案
是。你可以在这里阅读全文: https://docs.djangoproject.com/en/1.8/topics/auth/default/#django.contrib.auth.decorators.login_required ...但这里有一些要点:
Yes. You can read all about it here: https://docs.djangoproject.com/en/1.8/topics/auth/default/#django.contrib.auth.decorators.login_required ... but here are some bullet points:
- 添加
'django.contrib.auth.middleware.AuthenticationMiddleware'
toMIDDLEWARE_CLASSES
在settings.py
- 添加
'django.contrib.auth
'和'django.contrib.contenttypes'
到INSTALLED_APPS
在设置。 py
- 使用
django.contrib.auth.views.login
设置登录的URL视图,例如url(r'^ login / $','django.contrib.auth.views.login',name =my_login)
- 在您的看法中,包括login_required装饰器,并将其添加到视图之前。例如...
- add
'django.contrib.auth.middleware.AuthenticationMiddleware'
toMIDDLEWARE_CLASSES
insettings.py
- add
'django.contrib.auth
' and'django.contrib.contenttypes'
toINSTALLED_APPS
insettings.py
- setup a URL for the login using
django.contrib.auth.views.login
for the view, such asurl(r'^login/$', 'django.contrib.auth.views.login',name="my_login")
- In your view, include the login_required decorator and add it before your view. For example...
views.py ...
views.py...
from django.contrib.auth.decorators import login_required
@login_required
def home(request):
return HttpResponse('Home Page')
默认情况下,您将模板放在 my_template_directory / registration / login中。 html
。有关该模板的更多信息,请参阅本文开头的链接。
By default, you then put the template inside my_template_directory/registration/login.html
. Further info about that template can be found at the link in the beginning of this post.
这篇关于Django中有内置的登录模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!