一、前言
在上一篇文章中,博主給大家分享了如何基于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
評論
查看更多