【Python】Django--认证系统-登录注册

1.modles层 与admin

点击(此处)折叠或打开

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals

  3. from django.db import models
  4. from django.contrib.auth.models import AbstractUser
  5. from django.utils.encoding import python_2_unicode_compatible

  6. ###普通用户的数据模型
  7. @python_2_unicode_compatible
  8. class NewUser(AbstractUser):
  9.     profile = models.CharField('profile', default='', max_length=256)

  10.     def __str__(self):
  11.         return self.username
  12. # Create your models here.


##admin
# -*- coding: utf-8 -*- 
from django.contrib import admin from .models import NewUser class NewUserAdmin(admin.ModelAdmin):
    list_display = ('username','date_joined', 'profile')



admin.site.register(NewUser,NewUserAdmin)


2.setting设置newuser管理系统账号进行验证

  1. AUTH_USER_MODEL = "pric.NewUser"

3  views视图层

  1. # -*- coding: utf-8 -*-
  2. from django.shortcuts import render
  3. from .forms import *
  4. from .models import *
  5. from django.contrib.auth import authenticate, login, logout
  6. from django.http import HttpResponse
  7. from django.shortcuts import render, redirect,render_to_response
  8. from django.core.exceptions import ObjectDoesNotExist
  9. from django.contrib.auth.decorators import login_required



  10. @login_required
  11. def index(request):
  12.     return render_to_response('index.html', locals())




  13. ###登录
  14. def log_in(request):
  15.     if request.method == 'GET':
  16.         form = LoginForm()
  17.         return render(request, 'login.html', {'form': form})
  18.     if request.method == 'POST': # 当提交表单时
  19.         form = LoginForm(request.POST) # form 包含提交的数据
  20.         if form.is_valid(): # 如果提交的数据合法
  21.             username = form.cleaned_data['uid']
  22.             password = form.cleaned_data['pwd']
  23.             user = authenticate(username=username, password=password)  
  24.             if user is not None: ###如果用户验证存在,
  25.                 login(request, user) #开始登录
  26.                 return redirect('index') #跳转首页
  27.             else:
  28.                 return render(request, 'login.html', {'form': form, 'msg': "password or username is not ture!"})

  29.         else: ###如果提交的数据不合法,继续登录
  30.             return render(request, 'login.html', {'form': form})

  31. ###登出
  32. @login_required
  33. def log_out(request):
  34.     logout(request)
  35.     return redirect('login')
  36. # Create your views here.

  37. ###注册
  38. def register(request):
  39.     error1 = "this name is already exist"
  40.     valid = "this name is valid"

  41.     if request.method == 'GET':
  42.         form = RegisterForm()
  43.         return render(request, 'register.html', {'form': form})
  44.     if request.method == 'POST': ###提交
  45.         form = RegisterForm(request.POST)
  46.         if form.is_valid():
  47.                 username = form.cleaned_data['username']
  48.                 email = form.cleaned_data['email']
  49.                 password1 = form.cleaned_data['password1']
  50.                 password2 = form.cleaned_data['password2']
  51.                 if password1 != password2:
  52.                     return render(request, 'register.html', {'form': form, 'msg': "two password is not equal"})
  53.                 else:
  54.                     NewUser.objects.create_user(username=username,password=password1,email=email)
  55.                     #user = NewUser(username=username, email=email, password=password1)
  56.                     #user.save()
  57.                     # return render(request, 'login.html', {'success': "you have successfully registered!"})
  58.                     return redirect('login')
  59.         else:
  60.                 return render(request, 'register.html', {'form': form})

4 首页获取当前登录用户


方式一:
前端request.user.is_authenticated()判断当前用户是否登录 

<form class="form-signin" role="form" method='post'>
        <h2 class="form-signin-heading">Welcome!>           {% if request.user.is_authenticated %}
            hihi: {{ request.user }} </br>
          <a href="{% url 'log_out' %}">登出</a>
          {% endif %}
      </form>


方式二 将request.user.is_authenticated判断写入views视图再传入html中
@login_required
def index(request):
    if request.user.is_authenticated(): # 判断用户是否已登录
        user = request.user;
        return render_to_response('index.html', locals())



 <form class="form-signin" role="form" method='post'>
        <h2 class="form-signin-heading">Welcome!>
             欢迎登录!{{ user }}           <a href="{% url 'log_out' %}">登出</a>
          

      </form>





###本节学习的重点就是 authenticate认证
参考官方文档:http://python.usyiyi.cn/django/index.html
http://www.cnblogs.com/holbrook/archive/2012/02/10/2357345.html

----详细http://zmrenwu.com/post/31/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-2134408/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29096438/viewspace-2134408/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值