内容概要:
1.新增数据库表中数据
2.更新数据库表中数据
1.新增数据库表中数据
views文件夹==》mf.py中
‘‘‘ ModelForm的功能介绍: 1.获取所有或指定字段 2.新增数据库表中数据 3.更新数据库表中数据 ‘‘‘ from django.shortcuts import render from django import forms from app01 import models class UModelForm(forms.ModelForm): class Meta:#定义元类,同时能将关联的数据全部拿到(ForeignKey) model = models.User #和models.py中的User表关联,注意写法:model,没有s!!! fields = ‘__all__‘ #取全部字段 #新增数据方法,并且同步到数据库 def index(request): if request.method ==‘GET‘: obj =UModelForm() return render(request,‘mf.html‘,{‘obj‘:obj}) elif request.method ==‘POST‘: obj = UModelForm(request.POST) if obj.is_valid(): print(obj.cleaned_data) # models.User.objects.create(**obj.cleaned_data)#新增数据方法,或者用obj.save() obj.save() return render(request, ‘mf.html‘, {‘obj‘:obj})
mf.html中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post" action="/mf/"> {% csrf_token %} {{ obj.as_p }} <input type="submit" value="提交"> </form> </body> </html>
models.py中
from django.db import models class UserType(models.Model): name = models.CharField(max_length=32) def __str__(self): return self.name class User(models.Model): name = models.CharField(max_length=32) email = models.EmailField(max_length=32) u_type = models.ForeignKey(‘UserType‘) #一对多 m = models.ManyToManyField(‘M2M‘) #多对多 class M2M(models.Model): name = models.CharField(max_length=32) def __str__(self): return self.name
urls.py中
from django.conf.urls import url from django.contrib import admin from app01.views import fm from app01.views import mf urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^fm/‘, fm.index), url(r‘^aa/‘, fm.aa), url(r‘^mf/‘, mf.index), ]
2.更新数据库表中数据
views文件夹==》mf.py中
‘‘‘ ModelForm的功能介绍: 1.获取所有或指定字段 2.新增数据库表中数据 3.更新数据库表中数据 ‘‘‘ from django.shortcuts import render from django import forms from app01 import models class UModelForm(forms.ModelForm): class Meta:#定义元类,同时能将关联的数据全部拿到(ForeignKey) model = models.User #和models.py中的User表关联,注意写法:model,没有s!!! fields = ‘__all__‘ #取全部字段 #更新数据库中数据 def edit_index(request,nid): if request.method ==‘GET‘: model_obj = models.User.objects.get(id=nid) obj =UModelForm(instance=model_obj) #带上现在的值 return render(request,‘mf1.html‘,{‘obj‘:obj,‘nid‘:nid}) elif request.method == ‘POST‘: model_obj = models.User.objects.get(id=nid) obj = UModelForm(request.POST,instance=model_obj) #这个地方也要加上instance if obj.is_valid(): obj.save() return render(request, ‘mf1.html‘, {‘obj‘: obj})
mf1.html中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post" action="/edit-mf-{{ nid }}/"> {% csrf_token %} {{ obj.as_p }} <input type="submit" value="提交"> </form> </body> </html>
models.py中
from django.db import models class UserType(models.Model): name = models.CharField(max_length=32) def __str__(self): return self.name class User(models.Model): name = models.CharField(max_length=32) email = models.EmailField(max_length=32) u_type = models.ForeignKey(‘UserType‘) #一对多 m = models.ManyToManyField(‘M2M‘) #多对多 class M2M(models.Model): name = models.CharField(max_length=32) def __str__(self): return self.name
urls.py中
from django.conf.urls import url from django.contrib import admin from app01.views import fm from app01.views import mf urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^fm/‘, fm.index), url(r‘^aa/‘, fm.aa), url(r‘^mf/‘, mf.index), url(r‘^edit-mf-(\d+)‘, mf.edit_index), ]