python環境:python2.7
開發工具:pycharm
項目名稱:mysite5
app名稱:online
settings:映射app路徑

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'online', ]
數據庫:model:

1 # -*- coding: utf-8 -*- 2 from __future__ import unicode_literals 3 from django.db import models 4 5 # Create your models here.python manage.py syncdb 6 7 8 9 class User(models.Model): 10 username = models.CharField(max_length=50,unique=True) 11 password = models.CharField(max_length=50) 12 unique = True 13 def __unicode__(self): 14 return self.username
命令生成數據庫:
python manage.py makemigrations #生成配置文件
python manage.py migrate #生成表
python manage.py createsuperuser #生成超級用戶
項目下url.py匹配:#也稱一級路由

1 from django.conf.urls import url,include 2 from django.contrib import admin 3 from online import models,views 4 urlpatterns = [ 5 url(r'^admin/', admin.site.urls), 6 url(r'^online/', include('online.urls')), 7 8 ]
app下url.py:#也稱二級路由

1 from django.conf.urls import url,include 2 from django.contrib import admin 3 from online import models,views 4 urlpatterns = [ 5 url(r'^admin/', admin.site.urls), 6 url(r'^$', views.login, name='login'), 7 url(r'^login/$', views.login, name='login'), 8 url(r'^regist/$', views.regist, name='regist'), 9 url(r'^index/$', views.index, name='index'), 10 url(r'^logout/$', views.logout, name='logout'), 11 ]
view.py邏輯函數:#稱為視圖層

1 # -*- coding: utf-8 -*- 2 from __future__ import unicode_literals 3 4 from django.shortcuts import render 5 6 # Create your views here. 7 from django.shortcuts import render,render_to_response 8 from django.http import HttpResponse,HttpResponseRedirect 9 from django.template import RequestContext 10 from django import forms 11 from models import User 12 13 #表單 14 class UserForm(forms.Form): 15 username = forms.CharField(label='用戶名',max_length=100) 16 password = forms.CharField(label='密碼',widget=forms.PasswordInput()) 17 18 19 #注冊 20 def regist(req): 21 if req.method == 'POST': 22 uf = UserForm(req.POST) 23 if uf.is_valid(): 24 #獲得表單數據 25 username = uf.cleaned_data['username'] 26 password = uf.cleaned_data['password'] 27 #添加到數據庫 28 filterResult=User.objects.filter(username=username) 29 if len(filterResult)>0: 30 context={'error':'用戶名已存在,請從新輸入!'} 31 return render(req,'regist.html',context) 32 else: 33 User.objects.create(username= username,password=password) 34 return render(req,'regust_success.html') 35 else: 36 uf = UserForm() 37 return render(req,'regist.html',{'uf':uf}) 38 39 #登陸 40 def login(req): 41 if req.method == 'POST': 42 uf = UserForm(req.POST) 43 if uf.is_valid(): 44 #獲取表單用戶密碼 45 username = uf.cleaned_data['username'] 46 password = uf.cleaned_data['password'] 47 #獲取的表單數據與數據庫進行比較 48 user = User.objects.filter(username__exact = username,password__exact = password) 49 if user: 50 #比較成功,跳轉index 51 response = HttpResponseRedirect('/online/index/') 52 #將username寫入瀏覽器cookie,失效時間為3600 53 response.set_cookie('username',username,3600) 54 return response 55 else: 56 #比較失敗,還在login 57 return HttpResponseRedirect('/online/login/') 58 else: 59 uf = UserForm() 60 return render(req,'login.html',{'uf':uf}) 61 62 #登陸成功 63 def index(req): 64 username = req.COOKIES.get('username','') 65 return render(req,'index.html' ,{'username':username}) 66 67 #退出 68 def logout(req): 69 response = HttpResponse('logout !!') 70 #清理cookie里保存username 71 response.delete_cookie('username') 72 return render(req,'login.html')
項目下->templates #模板 存放HTML文件得
登錄頁面:index.html

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>登陸</title> 7 </head> 8 9 <body> 10 <h1>登陸頁面:</h1> 11 <form method = 'post' enctype="multipart/form-data"> 12 {% csrf_token %} 13 <p><label>用戶名:</label><input type="text" name="username" /></p> 14 <p><label>密    碼:</label><input type="password" name="password"/></p> 15 {# {{uf.as_p}}#} 16 <input type="submit" value = "ok" /> 17 </form> 18 <br> 19 <a href="http://127.0.0.1:8000/online/regist/">注冊</a> 20 </body> 21 </html>
注冊頁面:regist.html

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>注冊</title> 7 </head> 8 9 <body> 10 <h1>注冊頁面:</h1> 11 <form method = 'post' enctype="multipart/form-data"> 12 {% csrf_token %} 13 {{ error }} 14 <p><label>用戶名:</label><input type="text" name="username" /></p> 15 <p><label>密    碼:</label><input type="password" name="password"/></p> 16 <input type="submit" value = "ok" /> 17 </form> 18 <br> 19 <a href="http://127.0.0.1:8000/online/login/">登陸</a> 20 </body> 21 </html>
注冊成功頁面:login.html

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <p>注冊成功!</p> 9 <a href="http://127.0.0.1:8000/online/login/">登陸</a> 10 </body> 11 </html>
登錄成功頁面:index.html

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title></title> 7 </head> 8 9 <body> 10 <h1>welcome {{username}} !</h1> 11 <br> 12 <a href="http://127.0.0.1:8000/online/logout/">退出</a> 13 </body> 14 </html>
最后把服務器端口改成自己電腦得IP地址:
我是在pycharm里面改的
最后讓所有人都可以訪問:
在settings.py最下面添加
ALLOWED_HOSTS = ['*']