1.modles层 与admin
点击(此处)折叠或打开
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
-
- from django.db import models
- from django.contrib.auth.models import AbstractUser
- from django.utils.encoding import python_2_unicode_compatible
-
- ###普通用户的数据模型
- @python_2_unicode_compatible
- class NewUser(AbstractUser):
- profile = models.CharField('profile', default='', max_length=256)
-
- def __str__(self):
- return self.username
- # 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管理系统账号进行验证
- AUTH_USER_MODEL = "pric.NewUser"
3 views视图层
- # -*- coding: utf-8 -*-
- from django.shortcuts import render
- from .forms import *
- from .models import *
- from django.contrib.auth import authenticate, login, logout
- from django.http import HttpResponse
- from django.shortcuts import render, redirect,render_to_response
- from django.core.exceptions import ObjectDoesNotExist
- from django.contrib.auth.decorators import login_required
-
-
-
- @login_required
- def index(request):
- return render_to_response('index.html', locals())
-
-
-
-
- ###登录
- def log_in(request):
- if request.method == 'GET':
- form = LoginForm()
- return render(request, 'login.html', {'form': form})
- if request.method == 'POST': # 当提交表单时
- form = LoginForm(request.POST) # form 包含提交的数据
- if form.is_valid(): # 如果提交的数据合法
- username = form.cleaned_data['uid']
- password = form.cleaned_data['pwd']
- user = authenticate(username=username, password=password)
- if user is not None: ###如果用户验证存在,
- login(request, user) #开始登录
- return redirect('index') #跳转首页
- else:
- return render(request, 'login.html', {'form': form, 'msg': "password or username is not ture!"})
-
- else: ###如果提交的数据不合法,继续登录
- return render(request, 'login.html', {'form': form})
-
- ###登出
- @login_required
- def log_out(request):
- logout(request)
- return redirect('login')
- # Create your views here.
-
- ###注册
- def register(request):
- error1 = "this name is already exist"
- valid = "this name is valid"
-
- if request.method == 'GET':
- form = RegisterForm()
- return render(request, 'register.html', {'form': form})
- if request.method == 'POST': ###提交
- form = RegisterForm(request.POST)
- if form.is_valid():
- username = form.cleaned_data['username']
- email = form.cleaned_data['email']
- password1 = form.cleaned_data['password1']
- password2 = form.cleaned_data['password2']
- if password1 != password2:
- return render(request, 'register.html', {'form': form, 'msg': "two password is not equal"})
- else:
- NewUser.objects.create_user(username=username,password=password1,email=email)
- #user = NewUser(username=username, email=email, password=password1)
- #user.save()
- # return render(request, 'login.html', {'success': "you have successfully registered!"})
- return redirect('login')
- else:
- 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>
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/