django 框架实现的用户注册、登录、退出功能示例


Posted in Python onNovember 28, 2019

本文实例讲述了django 框架实现的用户注册、登录、退出功能。分享给大家供大家参考,具体如下:

1 用户注册:

from django.contrib import auth
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect
# 用户注册
@csrf_exempt
def register(request):
  errors = []
  account = None
  password = None
  password2 = None
  email = None
  CompareFlag = False
  if request.method == 'POST':
    if not request.POST.get('account'):
      errors.append('用户名不能为空')
    else:
      account = request.POST.get('account')
    if not request.POST.get('password'):
      errors.append('密码不能为空')
    else:
      password = request.POST.get('password')
    if not request.POST.get('password2'):
      errors.append('确认密码不能为空')
    else:
      password2 = request.POST.get('password2')
    if not request.POST.get('email'):
      errors.append('邮箱不能为空')
    else:
      email = request.POST.get('email')
    if password is not None:
      if password == password2:
        CompareFlag = True
      else:
        errors.append('两次输入密码不一致')
    if account is not None and password is not None and password2 is not None and email is not None and CompareFlag :
      user = User.objects.create_user(account,email,password)
      user.save()
      userlogin = auth.authenticate(username = account,password = password)
      auth.login(request,userlogin)
      return HttpResponseRedirect('/blog')
  return render(request,'blog/register.html', {'errors': errors})

2 用户登录:

@csrf_exempt
def my_login(request):
  errors =[]
  account = None
  password = None
  if request.method == "POST":
    if not request.POST.get('account'):
      errors.append('用户名不能为空')
    else:
      account = request.POST.get('account')
    if not request.POST.get('password'):
      errors = request.POST.get('密码不能为空')
    else:
      password = request.POST.get('password')
    if account is not None and password is not None:
      user = auth.authenticate(username=account,password=password)
      if user is not None:
        if user.is_active:
          auth.login(request,user)
          return HttpResponseRedirect('/blog')
        else:
          errors.append('用户名错误')
      else:
        errors.append('用户名或密码错误')
  return render(request,'blog/login.html', {'errors': errors})

3 用户退出:

def my_logout(request):
  auth.logout(request)
  return HttpResponseRedirect('/blog')

URL:

urlpatterns = [
  url(r'^$', views.index, name='index'),
  url(r'^p/(?P<article_id>[0-9]+)/$', views.detail,name='detail'),
  url(r'^register/$',views.register, name='register'),
  url(r'^login/$',views.my_login, name='my_login'),
  url(r'^logout/$',views.my_logout, name='my_logout'),
]

注册 HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
{% if errors %}
    <li>
      {% for error in errors %}
       <p style="color: red;">
        {{error}}
       </p>
       {% endfor %}
    </li>
  {% endif %}
<table>
  <form action="" method="post">{% csrf_token %}
    <tr>
      <td>
        <label >用户名:</label>
      </td>
      <td>
        <input type = 'text' placeholder="输入用户名" name = 'account'>
      </td>
    </tr>
    <tr>
      <td>
        <label >密码:</label>
      </td>
      <td>
       <input type = 'password' placeholder="输入密码" name = 'password'>
      </td>
    </tr>
     <tr>
       <td>
        <label >确认密码:</label>
       </td>
       <td>
         <input type = 'password' placeholder="再次输入密码" name ='password2'>
       </td>
     </tr>
     <tr>
       <td>
         <label>邮箱:</label>
       </td>
       <td>
         <input type="email" placeholder="输入邮箱" name = 'email'>
       </td>
     </tr>
     <tr>
       <td>
          <input type = 'submit' placeholder="Login" value="登录">
       </td>
     </tr>
  </form>
</table>
</body>
</html>

登录HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>登录</title>
</head>
<body>
{% if errors %}
    <li>
      {% for error in errors %}
       <p style="color: red;">
        {{error}}
       </p>
       {% endfor %}
    </li>
  {% endif %}
<table>
  <form action="" method="post">{% csrf_token %}
    <tr>
      <td>
        <label >用户名:</label>
      </td>
      <td>
        <input type = 'text' placeholder="输入用户名" name = 'account'>
      </td>
    </tr>
    <tr>
      <td>
        <label >密码:</label>
      </td>
      <td>
       <input type = 'password' placeholder="输入密码" name = 'password'>
      </td>
    </tr>
     <tr>
       <td>
          <input type = 'submit' placeholder="Login" value="登录">
       </td>
     </tr>
  </form>
</table>
</body>
</html>
</body>
</html>

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

Python 相关文章推荐
python解析xml模块封装代码
Feb 07 Python
Python使用ftplib实现简易FTP客户端的方法
Jun 03 Python
python3实现暴力穷举博客园密码
Jun 19 Python
Python中执行存储过程及获取存储过程返回值的方法
Oct 07 Python
Python数据类型之Number数字操作实例详解
May 08 Python
python绘制多个子图的实例
Jul 07 Python
django框架用户权限中的session缓存到redis中的方法
Aug 06 Python
Python产生一个数值范围内的不重复的随机数的实现方法
Aug 21 Python
pytorch 实现将自己的图片数据处理成可以训练的图片类型
Jan 08 Python
Django如何使用redis作为缓存
May 21 Python
python process模块的使用简介
May 14 Python
opencv 分类白天与夜景视频的方法
Jun 05 Python
python 变量初始化空列表的例子
Nov 28 #Python
在Python中预先初始化列表内容和长度的实现
Nov 28 #Python
python使用 cx_Oracle 模块进行查询操作示例
Nov 28 #Python
在python中创建指定大小的多维数组方式
Nov 28 #Python
python3.x 生成3维随机数组实例
Nov 28 #Python
python返回数组的索引实例
Nov 28 #Python
numpy中三维数组中加入元素后的位置详解
Nov 28 #Python
You might like
php _autoload自动加载类与机制分析
2012/02/10 PHP
ThinkPHP的截取字符串函数无法显示省略号的解决方法
2014/06/25 PHP
php上传图片之时间戳命名(保存路径)
2014/08/15 PHP
PHP多进程编程总结(推荐)
2016/07/18 PHP
PHP实现多图上传(结合uploadify插件)思路分析
2016/11/30 PHP
PHP针对redis常用操作实例详解
2019/08/17 PHP
JQUERY 浏览器判断实现函数
2009/08/20 Javascript
基于JQuery的密码强度验证代码
2010/03/01 Javascript
js对象的构造和继承实现代码
2010/12/05 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(四)用地图块拼成大地图
2013/01/23 Javascript
JavaScript中setInterval的用法总结
2013/11/20 Javascript
vue实现列表的添加点击
2016/12/29 Javascript
详解nodejs微信公众号开发——3.封装消息响应模块
2017/04/10 NodeJs
深入理解vue中的$set
2017/06/01 Javascript
让div运动起来 js实现缓动效果
2017/07/06 Javascript
jQuery UI Draggable + Sortable 结合使用(实例讲解)
2017/09/07 jQuery
nginx部署访问vue-cli搭建的项目的方法
2018/02/12 Javascript
使用typescript构建Vue应用的实现
2019/08/26 Javascript
解决layui批量传值到后台操作时出现传值为空的问题
2019/09/28 Javascript
适合前端Vue开发童鞋的跨平台Weex的使用详解
2019/10/16 Javascript
vue-cli或vue项目利用HBuilder打包成移动端app操作
2020/07/29 Javascript
Python Socket编程详细介绍
2017/03/23 Python
解决pandas read_csv 读取中文列标题文件报错的问题
2018/06/15 Python
对django 模型 unique together的示例讲解
2019/08/06 Python
使用selenium和pyquery爬取京东商品列表过程解析
2019/08/15 Python
python网络爬虫 CrawlSpider使用详解
2019/09/27 Python
python matplotlib工具栏源码探析三之添加、删除自定义工具项的案例详解
2021/02/25 Python
生物化工工艺专业应届生求职信
2013/10/08 职场文书
一名女生的自荐信
2013/12/08 职场文书
办加油卡单位介绍信
2014/01/09 职场文书
《生命 生命》教学反思
2014/04/19 职场文书
政府四风问题整改措施
2014/10/04 职场文书
给上级领导的感谢信
2015/01/22 职场文书
vue实现水波涟漪效果的点击反馈指令
2021/05/31 Vue.js
原型和原型链 prototype和proto的区别详情
2021/11/02 Javascript
浅析JavaScript中的变量提升
2022/06/01 Javascript