# 添加书籍: def add(reqeust): if GET请求: form=BookModelForm() return render(reqeust,{"form":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}) ''' HTML渲染页面代码 <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> '''