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

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

ubuntu安装sublime3并配置python3环境的方法

最近有一些烦,虚拟机跑代码,跑着跑着存储不够,我就去扩大磁盘,结果虚拟机崩了,试了一上午的修复办法,仍然无法修复,于是只能重装虚拟机,配置各种环境,这里总结一下Ubuntu中配置subl...

总结Python中逻辑运算符的使用

总结Python中逻辑运算符的使用

下表列出了所有Python语言支持的逻辑运算符。假设变量a持有10和变量b持有20,则:  示例: 试试下面的例子就明白了所有的Python编程语言提供了逻辑运算符:...

Python使用numpy实现BP神经网络

本文完全利用numpy实现一个简单的BP神经网络,由于是做regression而不是classification,因此在这里输出层选取的激励函数就是f(x)=x。BP神经网络的具体原理此...

django 外键model的互相读取方法

先设定一个关系模型如下: from django.db import models class Blog(models.Model): name = models.CharFiel...

Python常用的日期时间处理方法示例

#-*- coding: utf-8 -*- import datetime #给定日期向后N天的日期 def dateadd_day(days): d1 = datetim...