前2篇文章,散仙寫了關於Django的入門安裝,以及簡單模擬數據庫的MVC使用,那麼本篇就來稍微深刻下,來看看如何使用Django來實現一個增刪改查的小例子: html
序列 | 名稱 | 備註 |
1 | win7 | 操做系統 |
2 | 開發工具 | Pycharmpython |
3 | Python3.4 | python版本 |
4 | Django1.7 | Django版本 |
5 | SQLite | 數據庫 |
6 | 屌絲碼農一枚 | 核心角色 |
經過本案例項目,能學到什麼?
(1)表單post提交參數數據
(2)python對csrf的支持與應用
(3)增刪改查的處理思路
(4)python的Api熟悉
(5)python裏面重定向的使用
(6)模板文件的編寫格式
(7)mvt模式的瞭解
(8)python隔行換色的實現
(9)python對象關係映射使用
先看幾個案例的幾個截圖:
1,查詢全部:
2,添加一條數據:
3,修改數據:
4,刪除一條數據:
model裏的代碼: 程序員
from django.db import models # Create your models here. class Student(models.Model): name=models.CharField(max_length=20) age=models.IntegerField(max_length=3) class Subject(models.Model): student=models.ForeignKey(Student) sub_name=models.CharField(max_length=20) sub_num=models.IntegerField(default=0)
views裏面的代碼: 數據庫
import builtins from django.shortcuts import render,render_to_response from django.http import HttpResponse,HttpResponseRedirect from django.template.context import RequestContext #包裝csrf請求,避免django認爲其實跨站攻擊腳本 from django.views.decorators.csrf import csrf_exempt import random from.models import Student # Create your views here. from django.core.context_processors import csrf def hello(request): return HttpResponse("我是django的第一個例子!") def myhtml(request): return render_to_response('a.html',locals()) def bb(request): return render(request,'bb.html') #訪問首頁 def beginAdd(request): return render_to_response('add.html') #保存數據 @csrf_exempt def add(request): # c={} id=request.POST['id'] name=request.POST['name'] age=request.POST['age'] st=Student() if len(id) > 0 : print("id不是null") st.id=id; st.age=age st.name=name st.save() return HttpResponseRedirect("/q") #查詢全部 def query(request): b=Student.objects.all() #for e in b: #print(e.id," ",e.age," ",e.name) return render_to_response('curd.html',{'data':b}) #顯示一條數據 def showUid(request): id=request.GET['id']; bb=Student.objects.get(id=id) return render_to_response('update.html',{'data':bb}) #刪除數據 def delByID(request): id=request.GET['id']; bb=Student.objects.get(id=id) bb.delete() return HttpResponseRedirect("/q") datas=[ {"id":"1","name":"華爲"}, {"id":"2","name":"三星"}, {"id":"4","name":"Apple"}, {"id":"5","name":"中國"}, {"id":"6","name":"JAVA程序員"}, {"id":"7","name":"solr"}, {"id":"8","name":"hadoop編程"}, {"id":"9","name":"python"}, ] def show(request): return render_to_response('data.html',{'datas':datas})
url裏面的代碼: django
from django.conf.urls import patterns, include, url from django.contrib import admin #導入view定義的方法 from CurdWeb.views import hello,myhtml,bb,show,add,query,beginAdd,delByID,showUid urlpatterns = patterns('', # Examples: # url(r'^$', 'Django項目.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), #基於hellword的綁定 url(r'^hello/$',hello), url(r'^myhtml/$',myhtml), url(r'^cc/$',bb), #url映射到view層,並獲取展示數據 url(r'^show$',show), #添加數據映射 url(r'^add$',add), #查詢全部數據的映射 url(r'^q$',query), #訪問添加首頁的html url(r'^index.html$',beginAdd), #刪除用戶根據id url(r'delete$',delByID), #更新的方法,根據id url(r'showid$',showUid) )
html頁面 編程
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>添加數據,提交form表單</title> </head> <body> <form action="/add" method="post"> <input name="id" type="hidden" value="" ><br/> 請輸入名字<input name="name" type="text" ><br/> 請輸入年齡<input name="age" type="text" ><br/> <input type="submit" value="提交" > </form> </body> </html>
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>數據展現平臺</title> </head> <style> body{ text-align: center; } #tt{ margin: 0 auto; } </style> <body> <table id="tt" border="2"> <tr> <td>用戶編號</td> <td>用戶姓名</td> <td>用戶年齡</td> <td>操做</td> </tr> {% for d in data %} <tr {% if forloop.counter|divisibleby:"2" %} style="background: gainsboro" {% else %} style="background: aquamarine" {% endif %} > <td>{{ d.id }}</td> <td>{{ d.name }}</td> <td>{{ d.age }}</td><td>[url=/delete?id={{ d.id }}]刪除[/url] [url=/index.html]添加[/url] [url=/showid?id={{ d.id }}]修改[/url] </td> </tr> {% endfor %} </table> </body> </html>
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>動態展現學生信息數據</title> </head> <body> <table style="color: green" border="2"> <td>編號</td><td>名字</td> {% for m in datas %} <tr> <td>{{ m.id }}</td><td>{{ m.name }}</td> </tr> {% endfor %} </table> </body> </html>
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>修改我的信息</title> </head> <body> <form action="/add" method="post" > <input type="hidden" name="id" value="{{ data.id }}" > 名字:<input name="name" type="text" value="{{ data.name }}"><br/> 年齡:<input name="age" type="text" value="{{ data.age }}"><br/> <input type="submit" value="保存"/> </form> </body> </html>