第一;,models.py建表
from django.db import models# Create your models here.class Book(models.Model): #必须继承models.Model这个类,jango才能识别Book是一张表name=models.CharField(max_length=20)#charfield字符串类型,长度20#price=models.FloatField()price=models.IntegerField ()pub_date=models.DateField()author=models.CharField(max_length=32,null=False)class Author(models.Model):name=models.CharField(max_length=32)
第二 urls.py 建立连接
from django.conf.urls import url from django.contrib import adminfrom app import viewsurlpatterns = [url(r'^admin/', admin.site.urls),url(r'index',views.index),url(r'addbook',views.addbook),url(r'update',views.update),url(r'delete',views.delete),]
第三:views.py 写处理函数
from django.shortcuts import render,HttpResponse from app.models import *# Create your views here. def index(request):return render(request,"index.html") def addbook(request):#Book,使用导入from app.models import *b=Book(name="java",price=99,author="qiqi",pub_date="1456-12-12")b.save()#save执行,则上面语句成功#Book.objects.create(name="",。。。)另一种添加数据,不需要savereturn HttpResponse("添加成功")#要想此句需要from django.shortcuts import render,HttpResponse def update(request):Book.objects.filter(name="java").update(price="990")#修改方法一# #修改方法二# b=Book.objects.get(name="java")#拿到的b是列表类型是queryset集合的# b[0].price=100# b[0].save()return HttpResponse("修改成功") def delete(request):Book.objects.filter(name="java").delete()return HttpResponse("删除成功")
输入python manage.py rnserver 8800 访问127.0.0.1:8800/index
第四部:index.html
"en"> "UTF-8">Title