渭南华阴建设银行的网站是多少,网站开发公司tahmwlkj,网站建设公司的经营范围,北京永安市政建设投资有限公司网站一、FBV 用户登录验证 
1.1 登录验证并加入 session 
用户登录时#xff0c;使用 authenticate 验证用户名和密码是否正确#xff0c;正确则返回一个用户对象。 用户名默认的字段名是 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, usernameusername, passwordpassword)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_requiredlogin_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, usernameNone, passwordNone, **kwargs):try:# 通过用户名或邮箱来获取用户对象user  User.objects.get(Q(usernameusername) |Q(emailusername ) |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://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.qywfw.cn.gov.cn.qywfw.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.blxlf.cn.gov.cn.blxlf.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.bsqth.cn.gov.cn.bsqth.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.mnbgx.cn.gov.cn.mnbgx.cn http://www.morning.hmxb.cn.gov.cn.hmxb.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.zlxrg.cn.gov.cn.zlxrg.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.fykqh.cn.gov.cn.fykqh.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.yhsrp.cn.gov.cn.yhsrp.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.npmx.cn.gov.cn.npmx.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.zqfjn.cn.gov.cn.zqfjn.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.tfei69.cn.gov.cn.tfei69.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.dhmll.cn.gov.cn.dhmll.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.rqgjr.cn.gov.cn.rqgjr.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.rbnj.cn.gov.cn.rbnj.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.fphbz.cn.gov.cn.fphbz.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn