Django笔记-登陆、注册(利用cookie实现)
1、项目结构:
2、关键代码:
settings.py
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', )
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test10', 'HOST':'localhost', 'PORT':'3306', 'USER':'root', 'PASSWORD':'passwd', } }
urls.py
url(r'^register/$', 'blog.views.register'), url(r'^login/$','blog.views.login'), url(r'^index/$', 'blog.views.index'), url(r'^logout/$','blog.views.logout'),
models.py
# -*- coding: utf-8 -*- from django.db import models class User(models.Model): username = models.CharField(max_length=20) password = models.CharField(max_length=30) def __unicode__(self): return self.username
views.py
# -*- coding: utf-8 -*- from django.shortcuts import render, render_to_response from django.http import HttpResponse from django import forms from models import User from django.http.response import HttpResponseRedirect class UserForm(forms.Form): username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput) def register(req): #处理post方法的数据 if req.method == "POST": uf = UserForm(req.POST) #如果数据有效 if uf.is_valid(): username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] #username = unicode(uf.cleaned_data['username'],'UTF-8') #password = unicode(uf.cleaned_data['password'],'UTF-8') #print username,password #return HttpResponse('ok') User.objects.create(username=username,password=password) return HttpResponseRedirect('/login/') else: uf = UserForm() return render_to_response('register.html',{'uf':uf}) #处理get方法的数据 else : uf = UserForm(req.GET) if uf.is_valid(): username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] #username = unicode(uf.cleaned_data['username'],'UTF-8') #password = unicode(uf.cleaned_data['password'],'UTF-8') #print username,password #return HttpResponse('ok') User.objects.create(username=username,password=password) return HttpResponseRedirect('/login/') else: uf = UserForm() return render_to_response('register.html',{'uf':uf}) def login(req): if req.method == "POST": uf = UserForm(req.POST) if uf.is_valid(): username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] #获取名字和密码 users = User.objects.filter(username__exact=username,password__exact=password) if users: #获取到response response = HttpResponseRedirect('/index/') #保存到cookie response.set_cookie('username', username, 3600) return response else: return HttpResponseRedirect('/login/') #return HttpResponseRedirect('/login/') else: uf = UserForm() return render_to_response('login.html',{'uf':uf}) else : uf = UserForm(req.GET) if uf.is_valid(): username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] users = User.objects.filter(username__exact=username,password__exact=password) if users: #return HttpResponseRedirect('/index/') response = HttpResponseRedirect('/index/') #response.set_signed_cookie('username', username, 3600) response.set_cookie('username',username,3600) else: return HttpResponseRedirect('/login/') #return HttpResponseRedirect('/login/') else: uf = UserForm() return render_to_response('login.html',{'uf':uf}) def index(req): #return HttpResponse('ok') username = req.COOKIES.get('username','') #return HttpResponse('welcome %s'% username) return render_to_response('index.html',{'username':username}) def logout(req): # 删除cookie response = HttpResponse('logout') response.delete_cookie('username') return response
register.html
<body> <form method="post"> {{uf.as_p}} <input type="submit" value="OK"/> </form> </body>
login.html
<body> <form method="post"> {{uf.as_p}} <input type="submit" value="OK"/> </form> </body>
index.html
<body> <div> <h1>welcome , {{username}}</h1> <a href='/logout/'>logou</a> </div> </body>
3、效果截图:
注册:
登陆:
登陆成功:
注销:
再次输入index主体页面,已经删除了cookie,效果如下:
在数据库中插入的数据(推荐使用SQLyog,这个真心不错):