Django中有内置的登录模板吗? [英] Is there a built-in login template in Django?

查看:159
本文介绍了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' to MIDDLEWARE_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' to MIDDLEWARE_CLASSES in settings.py
  • add 'django.contrib.auth' and 'django.contrib.contenttypes' to INSTALLED_APPS in settings.py
  • setup a URL for the login using django.contrib.auth.views.login for the view, such as url(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆