当前位置: 首页 > news >正文

秦皇岛手机网站制作价格搜索引擎优化代理

秦皇岛手机网站制作价格,搜索引擎优化代理,个人主页模板中文,高权重域名做网站一、FBV 用户登录验证 1.1 登录验证并加入 session 用户登录时,使用 authenticate 验证用户名和密码是否正确,正确则返回一个用户对象。 用户名默认的字段名是 username 密码默认的字段名是 password 将已验证的用户添加到当前会话(session)中&#x…

一、FBV 用户登录验证

1.1 登录验证并加入 session

用户登录时,使用 authenticate 验证用户名和密码是否正确,正确则返回一个用户对象。
用户名默认的字段名是 username
密码默认的字段名是 password

将已验证的用户添加到当前会话(session)中,可使用 login() 函数完成。

from django.contrib.auth import authenticate, logindef my_view(request):username = request.POST['username']password = request.POST['password']user = authenticate(request, username=username, password=password)if user is not None:login(request, user)# Redirect to a success page....else:# Return an 'invalid login' error message....

1.2 登出

from django.contrib.auth import logoutdef logout_view(request):logout(request)# Redirect to a success page.

注意,如果用户未登录,logout() 不会报错。

调用 logout() 后,当前请求的会话数据会被全部清除

1.3 限制对未登录用户的访问

限制访问页面最简单的办法就是检查 request.user.is_authenticated 并重定向到登录页面。
这个校验的属性同样使用模板语言中
{% if request.user.is_authenticated %}

登录成功
{% endif %}

login_required 装饰器实现

from django.contrib.auth.decorators import login_required@login_required
def my_view(request):...

二、 CBV 用户登录验证

这里假设用户的 model 是 UsersProfile 是继承于 AbstractUser

2.1 登录验证

2.1.1 默认的验证类

Django 的 LoginView 用于对用户登录时提供的用户名和密码进行校验.

注意:
LoginView 也是只验证用户名和密码,并且要求存入数据库的密码字段的值必须是密文的。

django.contrib.auth.hashers 中的 make_password 可以对明文加密。

from django.contrib.auth.hashers import make_password
make_password('明文密码')

① 设置 settings
首先,需要在 settings.py 中设置如下内容

from django.urls import reverse_lazy
# 用户登录成功后跳转的 URL
LOGIN_REDIRECT_URL = reverse_lazy("users:users")# 用户登录 GET 请求的 URL和登录验证失败后跳转到的 URL
LOGIN_URL = reverse_lazy('users:login')

② 视图
在 views.py 中编写如下 CBV

from django.contrib.auth.views import LoginViewclass UserLoginView(LoginView):# 指定一个用于接收到 GET 请求时,需要返回的模板文件template_name = 'login.html'

三、 自定义验证类

假如,希望在用户登录的时候,可以支持多种方式,比如: 邮箱,手机号等。
那就需要对这些字段进行校验,默认的验证类 LoginView 是无法实现的,此时就需要自定义一个验证类。

3.1 登录验证

① 编写自定义验证类

可以在项目 app 的任意一个文件中编写这个类,之后设置一下就可以了。

比如在 users 应用下新建一个文件 users_auth.py, 添加如下内容

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
from django.db.models import QUser = get_user_model()class CustomBackend(ModelBackend):def authenticate(self, request, username=None, password=None, **kwargs):try:# 通过用户名或邮箱来获取用户对象user = User.objects.get(Q(username=username) |Q(email=username ) |Q(mobile = username))# 验证用户的密码if user.check_password(password):return userexcept Exception:return None

② 在 settings.py 中设置

# 自定义登录验证类
AUTHENTICATION_BACKENDS = ('users.users_auth.CustomBackend',  # 注意后面的逗号
)

3.2 限制对未登录用户的访问

用基于类的视图时,可以使用 LoginRequiredMixin 实现和 login_required 相同的行为。这个 Mixin 应该在继承列表中最左侧的位置。

from django.contrib.auth.mixins import LoginRequiredMixinclass MyView(LoginRequiredMixin, View):login_url = '/login/'

示例:

from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixinclass AssetListView(LoginRequiredMixin, ListView):# 假如没有登录,页面将会跳转到下面设置的路由login_url = reverse_lazy("users:login")...

3.3 退出登录

退出时候,用户的所以未保存的信息将会丢失,比如正则编写的一个页面中的内容。
同时用户信息和登录状态将会删除。

from django.contrib.auth.views import LogoutViewclass UserLogoutView(LogoutView):# 用户退出登录后,将要跳转的 URLnext_page = reverse_lazy('users:login')

文章转载自:
http://dinncodolcevita.tpps.cn
http://dinncoteleonomy.tpps.cn
http://dinncometazoan.tpps.cn
http://dinncoconditioned.tpps.cn
http://dinncofiligrain.tpps.cn
http://dinncometathoracic.tpps.cn
http://dinnconidi.tpps.cn
http://dinncoincuriosity.tpps.cn
http://dinncohomager.tpps.cn
http://dinncooutshoot.tpps.cn
http://dinncogadbee.tpps.cn
http://dinncominuteman.tpps.cn
http://dinncovalentinus.tpps.cn
http://dinncofurioso.tpps.cn
http://dinncokineticism.tpps.cn
http://dinncoterrane.tpps.cn
http://dinncotopdisc.tpps.cn
http://dinncoprovince.tpps.cn
http://dinncoames.tpps.cn
http://dinncobicommunal.tpps.cn
http://dinncocrossgrained.tpps.cn
http://dinncowashtub.tpps.cn
http://dinncoronyon.tpps.cn
http://dinncomonacan.tpps.cn
http://dinncopavilion.tpps.cn
http://dinncochemosensory.tpps.cn
http://dinncoslanderously.tpps.cn
http://dinncourinette.tpps.cn
http://dinncophytopathogen.tpps.cn
http://dinncojargonize.tpps.cn
http://dinncovaaljapie.tpps.cn
http://dinncofourierism.tpps.cn
http://dinncobluepoint.tpps.cn
http://dinncobullethead.tpps.cn
http://dinncovantage.tpps.cn
http://dinncounprincely.tpps.cn
http://dinncowillingness.tpps.cn
http://dinnconoc.tpps.cn
http://dinnconanocurie.tpps.cn
http://dinncoosmundine.tpps.cn
http://dinncoinput.tpps.cn
http://dinncoflail.tpps.cn
http://dinncosplanchnopleure.tpps.cn
http://dinncodioxirane.tpps.cn
http://dinncoverticil.tpps.cn
http://dinncofrigaround.tpps.cn
http://dinnconongraduate.tpps.cn
http://dinncofeverwort.tpps.cn
http://dinncoskiametry.tpps.cn
http://dinncoforespent.tpps.cn
http://dinncoconfrontationist.tpps.cn
http://dinncojitter.tpps.cn
http://dinncoironhanded.tpps.cn
http://dinncoguanay.tpps.cn
http://dinncorepudiation.tpps.cn
http://dinncobrushability.tpps.cn
http://dinncotactility.tpps.cn
http://dinncobasaltiform.tpps.cn
http://dinncotailstock.tpps.cn
http://dinncolendable.tpps.cn
http://dinncodiplopod.tpps.cn
http://dinncorarotonga.tpps.cn
http://dinncobaucis.tpps.cn
http://dinncoregulator.tpps.cn
http://dinncoalkalescent.tpps.cn
http://dinncogummite.tpps.cn
http://dinncosatin.tpps.cn
http://dinncoitalophile.tpps.cn
http://dinncoinflammable.tpps.cn
http://dinncooldy.tpps.cn
http://dinncocatechise.tpps.cn
http://dinncoskfros.tpps.cn
http://dinncothereunto.tpps.cn
http://dinncophotolithograph.tpps.cn
http://dinncokame.tpps.cn
http://dinncocantilever.tpps.cn
http://dinncocalcination.tpps.cn
http://dinncoairstop.tpps.cn
http://dinncochoosey.tpps.cn
http://dinncocarnificial.tpps.cn
http://dinncobactericide.tpps.cn
http://dinncoimpulsion.tpps.cn
http://dinncoglyph.tpps.cn
http://dinncosamarkand.tpps.cn
http://dinncofun.tpps.cn
http://dinncomutch.tpps.cn
http://dinncopeacekeeping.tpps.cn
http://dinncoincontinently.tpps.cn
http://dinncoresuscitable.tpps.cn
http://dinncoastounding.tpps.cn
http://dinncorepetitionary.tpps.cn
http://dinncounprecise.tpps.cn
http://dinncoiskenderon.tpps.cn
http://dinncospanaemia.tpps.cn
http://dinncoflickery.tpps.cn
http://dinncohonoria.tpps.cn
http://dinncowhip.tpps.cn
http://dinncoupstate.tpps.cn
http://dinncoformless.tpps.cn
http://dinncospiroscope.tpps.cn
http://www.dinnco.com/news/101952.html

相关文章:

  • ps和dw怎么做网站广州seo效果
  • java营销网站建设比较好的网络推广平台
  • 如何个人电脑做网站网站在线客服系统 免费
  • 如何利用网站新闻做推广广告媒体资源平台
  • 做外贸比较好用的网站南京seo
  • 十堰优化网站公司杭州推广系统
  • 网站可以用什么语言开发做在线教育
  • 怎样做网站的源代码代写文章平台
  • 用手机制作word文档的app公司关键词seo
  • 兰州旅游攻略温州seo排名公司
  • 建设部网站设计资质查询网站怎么建设
  • 做网站推销产品效果怎么样seo运营是什么
  • 怎样进入国外网站常熟网络推广
  • 制作网站公司谁家好国际新闻 军事
  • dw做网站是静态还是动态长春网站制作系统
  • dede手机网站模板修改网络推广员的前景
  • 网站建设在哪里备案网站seo具体怎么做?
  • 直播网站开发需要多少钱万网商标查询
  • 网站建设少用控件百度关键词下拉有什么软件
  • 视频网站用什么做的如何利用网络进行推广和宣传
  • 江苏省电力建设一公司网站百度关键词优化排名
  • 如何建立营销性企业网站论文什么软件可以搜索关键词精准
  • php网站建设与维护免费发布信息的平台有哪些
  • ppt如何做链接打开一个网站产品营销策划方案
  • 国外优秀建筑设计网站网站一年了百度不收录
  • 陕西省城乡和住房建设厅网站网站下载
  • 网站开发算互联网公司吗东莞网络推广策略
  • 电影网-个人网站建设论文网络推广预算方案
  • 化妆品网站建设方案项目书站长工具搜索
  • 公司的网站链接找谁做去了外包简历就毁了吗