侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Django框架使用内置方法实现登录功能详解

框架(架构)  /  管理员 发布于 7年前   161

本文实例讲述了Django框架使用内置方法实现登录功能。分享给大家供大家参考,具体如下:

一 内置登录退出思维导图

二 Django内置登录方法

1 位置

2 源码

@deprecate_current_app@sensitive_post_parameters()@csrf_protect@never_cache# 视图函数要渲染的模板位置(registration/login.html)def login(request, template_name='registration/login.html',     redirect_field_name=REDIRECT_FIELD_NAME,     authentication_form=AuthenticationForm,     extra_context=None, redirect_authenticated_user=False):  """  Displays the login form and handles the login action.  """  redirect_to = request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, ''))  if redirect_authenticated_user and request.user.is_authenticated:    redirect_to = _get_login_redirect_url(request, redirect_to)    if redirect_to == request.path:      raise ValueError(        "Redirection loop for authenticated user detected. Check that "        "your LOGIN_REDIRECT_URL doesn't point to a login page."      )    return HttpResponseRedirect(redirect_to)  elif request.method == "POST":    form = authentication_form(request, data=request.POST)    if form.is_valid():      auth_login(request, form.get_user())      return HttpResponseRedirect(_get_login_redirect_url(request, redirect_to))  else:    form = authentication_form(request)  current_site = get_current_site(request)  context = {    'form': form,    redirect_field_name: redirect_to,    'site': current_site,    'site_name': current_site.name,  }  if extra_context is not None:    context.update(extra_context)  return TemplateResponse(request, template_name, context)

三 实战一 

1 编辑mysite/account/urls.py

from django.conf.urls import urlfrom . import viewsfrom django.contrib.auth import views as auth_viewsurlpatterns = [  # 自定义登录  # url(r'^login/$', views.user_login, name='user_login'),  # django内置的登录  url(r"^login/$", auth_views.login, name="user_login"),]

2 因为默认的模板位置为registration/login.html,因此我们创建该文档如下:

{% extends "base.html" %}{% block title %}登录{% endblock %}{% block content %}<div class="row text-center vertical-middle-sm"> <h1>登录</h1> <p>请输入用户名和密码</p>  <!--用具体的URL指明了数据的POST目标--> <form class="form-horizontal" action="{% url 'account:user_login' %}" method="post"> {% csrf_token %}    <!--每个表单元素在一对P标签内-->    <!--{{ form.as_p }}-->    <!--使用Bootstrap样式使得表单更美丽-->    <div class="form-group">  <label for="{{ form.username.id_for_label }}" class="col-md-5 control-label" style="color:red"><span class="glyphicon glyphicon-user"></span>Username</label>  <div class="col-md-6 text-left">{{ form.username }}</div> </div> <div class="form-group">  <label for="{{ form.password.id_for_label }}" class="col-md-5 control-label" style="color:blue"><span class="glyphicon glyphicon-floppy-open"></span>Password</label>  <div class="col-md-6 text-left">{{ form.password }}</div> </div> <input type="submit" value="Login"> </form></div>{% endblock %}

3 修改mysite/mysite/settings.py

# 登录后重定向到http://localhost:8000/blog/页面LOGIN_REDIRECT_URL = '/blog/'

4 测试

四 实战二

1 编辑mysite/account/urls.py

from django.conf.urls import urlfrom . import viewsfrom django.contrib.auth import views as auth_viewsurlpatterns = [  # 自定义登录  # url(r'^login/$', views.user_login, name='user_login'),  # django内置的登录  url(r"^login/$", auth_views.login, name="user_login"),  url(r"^new-login/$", auth_views.login, {"template_name": "account/login.html"}),]

2 测试

希望本文所述对大家基于Django框架的Python程序设计有所帮助。


  • 上一条:
    三步实现Django Paginator分页的方法
    下一条:
    用pycharm开发django项目示例代码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客