一 modelForm
(1) model的知识点:
class UserInfo(AbstractUser):
tel=models.CharField(max_length=32)
gender=models.IntegerField(choices=((1,"男"),(2,"女")),default=1)
yuan=UserInfo.objects.get(pk=1)
yuan.get_gender_display()
(2) modelform使用
model.py:
class Book(models.Model):
nid=models.AutoField(primary_key=True)
title=models.CharField(max_length=32)
price=models.DecimalField(max_digits=8,decimal_places=2) # 999999.99
pub_date=models.DateTimeField() # "2012-12-12"
# comment_count=models.IntegerField(default=100)
# poll_count=models.IntegerField(default=100)
publish=models.ForeignKey(to="Publish",on_delete=models.CASCADE) # 级联删除
authors=models.ManyToManyField(to="Author")
def __str__(self):
return self.title
form.py:
# 构建modelform
class BookModelForm(forms.ModelForm):
class Meta:
model=Book
fields="__all__"
'''
BookModelForm等同于:
class BookForm(forms.Form):
title=forms.CharField(max_length=32)
price=forms.IntegerField()
pub_date=forms.DateField(widget=widgets.TextInput(attrs={"type":"date"}))
#publish=forms.ChoiceField(choices=[(1,"AAA"),(2,"BBB")])
publish=forms.ModelChoiceField(queryset=Publish.objects.all())
authors=forms.ModelMultipleChoiceField(queryset=Author.objects.all())
'''
逻辑:
添加书籍:
def add(reqeust):
if GET请求:
form=BookModelForm()
return render(reqeust,{"form":form})
'''
渲染页面
<form action="" method="post" novalidate>
{% csrf_token %}
{% for field in form %}
<div class="form-group">
<label for="title">{{ field.label }}</label>
{{ field }}
<span>{{ field.errors.0 }}</span>
</div>
{% endfor %}
<input type="submit" value="提交" class="btn btn-default pull-right">
</form>
'''
else POST请求:
form=BookModelForm(request.POST)
if form.is_valid():
form.save() # Book.objects.create(clean_data)
return redirect("/")
else:
return render(reqeust,{"form":form})
编辑书籍:
def edit(request,id):
edit_obj=Book.objects.get(pk=id)
if GET请求:
form=BookModelForm(instance=edit_obj)
return render(reqeust,{"form":form})
'''
渲染页面同添加页面
'''
else POST请求:
form=BookModelForm(request.POST,instance=edit_obj)
if form.is_valid():
form.save() # edit_obj.update(clean_data)
return redirect("/")
else:
return render(reqeust,{"form":form})
今天博客:
https://www.cnblogs.com/yuanchenqi/articles/7614921.html
https://www.cnblogs.com/yuanchenqi/articles/8034442.html#_label1