一、前言
在上一篇文章中,博主给大家分享了如何基于410c利用python的Django框架去构建自己的局域网web服务器,今天我们在其基础上再扩展建立一个局域网的用户管理系统。
二、代码分析
1.代码框架
图1 代码框架
2.主代码分享:
2.1.online/view.py
from django.shortcuts import render,render_to_response
from django.http import HttpResponse,HttpResponseRedirect
from django.template import RequestContext
from online.models import User
from django.contrib.auth import authenticate, login
#表单
class UserForm(forms.Form):
username = forms.CharField(label='用户名',max_length=100)
password = forms.CharField(label='密码',widget=forms.PasswordInput())
#def getuser(forms):
# return forms.username
# password1 = forms.CharField(label='重复密码',widget=forms.TextInput())
# loginaddr="http://127.0.0.1:8000/online/login/"
#注册
def regist(req):
if req.method == 'POST':
uf = UserForm(req.POST)
if uf.is_valid():
#获得表单数据
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
password1 = req.POST['password1']
#比较数据库中是否已有用户
#user = myUser.objects.all().filter(username = username)
#print(user)
if User.objects.filter(username =username).exists():
return render_to_response('registfaild.html',{'uf':uf}, context_instance=RequestContext(req))
else:
if password1==password:
# User.objects.create(username= username,password=password)#添加用户进数据库
# profile=UserProfile()#e*************************
# profile.user_id=user.id
# profile.phone=phone
# profile.save()
# user=User()
# user.username=username
# user.password=password
# user.save()
user = User.objects.create_user(username,password)
user.save()
response = HttpResponseRedirect('/online/registsuccee/')
return response
else:
return render_to_response('registfaild1.html',{'uf':uf}, context_instance=RequestContext(req))
else:
uf = UserForm()
return render_to_response('regist.html',{'uf':uf}, context_instance=RequestContext(req))
#登陆
def login(req):
# getAllUser(req)
if req.method == 'POST':
uf = UserForm(req.POST)
if uf.is_valid():
#获取表单用户密码
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
#获取的表单数据与数据库进行比较
user = authenticate(username=username, password=password)
if user is not None:
#比较成功,跳转index
response = HttpResponseRedirect('/online/index/')
#将username写入浏览器cookie,失效时间为3600
response.set_cookie('username',username,3600)
return response
else:
return render_to_response('loginfaild.html',{'uf':uf},context_instance=RequestContext(req))
else:
uf = UserForm()
return render_to_response('login.html',{'uf':uf},context_instance=RequestContext(req))
#def loginfaild(req):
# response = login(req)
# return response
#
def registsuccee(req):
if req.method == 'POST':
uf = UserForm(req.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('/online/index/')
#将username写入浏览器cookie,失效时间为3600
response.set_cookie('username',username,3600)
return response
else:
#比较失败,跳转loginfaild
return render_to_response('loginfaild.html',{'uf':uf},context_instance=RequestContext(req))
else:
uf = UserForm()
#return render_to_response('registfaild.html',{'uf':uf},context_instance=RequestContext(req))
return render_to_response('registsuccee.html',{'uf':uf},context_instance=RequestContext(req))
#
#def registfaild(req):
# response = regist(req)
# return response
#
#def registfaild1(req):
# response = regist(req)
# return response
#登陆成功
def index(req):
username = req.COOKIES.get('username','')
user_list=getAllUser(req)
if req.method == 'POST':
key_user=req.POST.get('username')
key_input=req.POST.get('text')
print(key_user)
print(key_input)
if key_input=='text':
print('please input text')
response = HttpResponseRedirect('/online/text/')
#将username写入浏览器cookie,失效时间为3600
response.set_cookie('username',username,3600)
return response
elif key_input=='video':
print('please input video')
response = HttpResponseRedirect('/online/video/')
#将username写入浏览器cookie,失效时间为3600
response.set_cookie('username',username,3600)
return response
else:
return render_to_response('loginsuccee.html' ,{'user_list':user_list,'username':username},)
# user_sum=user_list.count()
# if req.method == 'POST':
# if req.POST['username']=="ad3600":
# if req.post.get('text')=="video":
return render_to_response('loginsuccee.html' ,{'user_list':user_list,'username':username},)
#退出
def logout(req):
response = login(req)
response.delete_cookie('username')
return response
#response = HttpResponse('logout !!')
#清理cookie里保存username
# response.delete_cookie('username')
# return response
def getAllUser(req):
user_list=User.objects.all().values('username')
user_sum=User.objects.all().values('username').count()
print(user_list)
print(user_sum)
return user_list
def text(req):#输入文本处理
username = req.COOKIES.get('username','')
if req.method == 'POST':
text = req.POST['text_input']
print(req.user.is_anonymous())
req.user.text=text
req.user.username=username
print(username)
print(req.user.get_full_name())
print(req.user.text)
req.user.save()
# print(dir(MyUser))
return render_to_response('text.html',)
return req
def delete():
User.objects.filter().delete()
2.2.online/urls
from django.conf.urls import patterns, url
from online import views
import register.settings
urlpatterns = patterns('',
url(r'^$', views.login, name='login'),
url(r'^login/$',views.login,name = 'login'),
url(r'^loginfail/$',views.login,name = 'loginfail'),
url(r'^regist/$',views.regist,name = 'regist'),
url(r'^registfaild/$',views.regist,name = 'regist'),
url(r'^registfaild1/$',views.regist,name = 'regist'),
url(r'^registsuccee/$',views.registsuccee,name = 'registsuccee'),
url(r'^text/$',views.text,name = 'text'),
url(r'^video/$',views.video,name = 'video'),
url(r'^index/$',views.index,name = 'index'),
url(r'^logout/$',views.logout,name = 'logout'),
url(r'^stat
ic/(?P
.*)$', 'django.views.static.serve',{ 'document_root': register.settings.STATIC_URL }),
)
2.3.online/modes
#coding:utf8
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings
# Create your models here.后台数据保存
# Create your models here.
class User(models.Model):
# nickname = models.CharField(max_length=64, blank=False)
# author = models.ForeignKey(settings.AUTH_USER_MODEL)
text = models.CharField(max_length=50)
email= models.EmailField()
# desc = models.TextField()
# qq = models.CharField(u'qq号', max_length=16)
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
def __unicode__(self):
return self.username
#class UserAdmin(admin.ModelAdmin):
# list_display = ('username','password')
# Create your models here.
#class MyUser(AbstractUser):
# qq = models.CharField(u'qq号', max_length=16)
# weChat =models.CharField(u'微信账号', max_length=100)
# mobile =models.CharField(u'手机号', primary_key=True, max_length=11)
# identicard =models.BooleanField(u'身份证认证', default=False) #默认是0,未认证, 1:身份证认证, 2:视频认证
# refuserid = models.CharField(u'推荐人ID', max_length=20)
# Level = models.CharField(u'用户等级', default='0', max_length=2) #默认是0,用户等级0-9
# vevideo = models.BooleanField(u'视频认证', default=False) #默认是0,未认证。 1:已认证
# Type =models.CharField(u'用户类型', default='0', max_length=1) #默认是0,未认证, 1:刷手 2:商家
#
# def __str__(self):
# return self.username
三、实测效果
图2 登录页面
图3 注册页面
四、例程分享:http://pan.baidu.com/s/1dFOKoI9
评论
查看更多