1.使用之前创建的项目和应用 mysite3 account
2.使用之前的数据库构造
class User(models.Model): username=models.CharField(max_length=50) password=models.CharField(max_length=50) email=models.EmailField() def __unicode__(self): return self.username class UserAdmin(admin.ModelAdmin): list_display=('username','email') admin.site.register(User,UserAdmin)
3.使用之前的项目urls.py
from django.conf.urls import url,include from django.contrib import admin admin.autodiscover() urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^account/', include('account.urls')), ]
应用的urls.py
from django.conf.urls import url from account import views urlpatterns=[ url(r'^$',views.login,name='login'), url(r'^register/$',views.register,name='register'), url(r'^login/$',views.login,name='login'), url(r'^index/$',views.index,name = 'index'), url(r'^logout/$',views.logout,name = 'logout'), ]
http://127.0.0.1:8000/online/ 登陆页
http://127.0.0.1:8000/online/login/ 登陆页
http://127.0.0.1:8000/online/register/ 注册页
http://127.0.0.1:8000/online/index/ 登陆成功页
http://127.0.0.1:8000/online/logout/ 注销
4.修改views.py
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render,render_to_response from django.template import loader,Context,RequestContext from django.http import HttpResponse,HttpResponseRedirect from django import forms from account.models import User # Create your views here. #定义表单模型 —— 在页面上显示的输入框 class UserForm(forms.Form): username=forms.CharField(label='用户名:',max_length=100) password=forms.CharField(label='密码:',widget=forms.PasswordInput()) email=forms.EmailField(label='电子邮件:') class User2Form(forms.Form): username=forms.CharField(label='用户名:',max_length=100) password=forms.CharField(label='密码:',widget=forms.PasswordInput()) #注册 def register(request): if request.method=='POST': uf=UserForm(request.POST) if uf.is_valid(): #获取表单信息 username=uf.cleaned_data['username'] password=uf.cleaned_data['password'] email=uf.cleaned_data['email'] #将表单数据写入数据库 #user=User() #user.username=username #user.password=password #user.email=email #user.save() User.objects.create(username= username,password=password,email=email) #url不变,跳到新页面显示register success return HttpResponse('register success!!!') else: uf=UserForm() #注册失败,重新回到注册页,最后一截的意思是显示用户输入用户名密码 #render_to_response()加载模板 返回 HttpResponse 对象,因此我们仅需在视图中return 该值 return render_to_response('register.html',{'uf':uf}) #登陆 def login(request): if request.method=='POST': uf =User2Form(request.POST) if uf.is_valid(): #获取表单用户密码 username=uf.cleaned_data['username'] password=uf.cleaned_data['password'] #获取的表单数据与数据库做比较 user=User.objects.filter(username__exact=username,password__exact=password) if user: #比较成功,跳转index response=HttpResponseRedirect('/account/index/') #将username写入浏览器cookie,失效时间未3600 response.set_cookie('username',username,3600) return response else: #比较失败,回到login return HttpResponseRedirect('account/login/') else: uf=User2Form() return render_to_response('login.html',{'uf':uf}) #登陆成功 def index(request): username=request.COOKIES.get('username','') return render_to_response('index.html',{'username':username}) #退出 def logout(request): response=HttpResponse('logout!!!') #清理cookie里保存的username response.delete_cookie('username') return response
5.创建模板