【发布时间】:2017-05-26 17:58:26
【问题描述】:
我正在尝试在 Django HTML 模板中显示一个表单,但它没有被渲染
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Description, Bill
from django.http import Http404
from .forms import DForm
from .forms import BForm
import pprint
# Create your views here.
def index(request):
context = {}
return render(request, 'front/index.html', context)
def commitbill(request):
if request.method == "POST":
form = BForm(request.POST,request.FILES)
if form.is_valid():
print form.errors
Bill = form.save()
return HttpResponse(str(Bill.bill_id()))
print form.errors
return HttpResponse("fail")
forms.py
from django import forms
from .models import Description, Bill
class BForm(forms.ModelForm):
class Meta:
db_table = 'inventory_bill'
model = Bill
fields = ('party', 'inovice', 'amount','image','image_caption')
模板,其中一部分没有被渲染!
{% load staticfiles %}
<html>
<head>
<title></title>
<link href="{%static "./styles/bootstrap.min.css" %}" rel="stylesheet" />
</head>
<body>
<h1 style="text-align: center;">Rahul's Shop</h1>
<h2 style="text-align: center;">Inventory</h2>
<form id="bill" action ="{% url 'front:commitbill' %}" method = "post" class="form-horizontal" enctype="multipart/form-data">
{% csrf token %}
{{ form.as_p }}
<div class="container">
<div class="row">
<input type="button" id="add_items" class="col-md-offset-5 col-md-2 btn btn-success" value="Add items" \>
</div>
</div>
</form>
{{ form.as_p }} 部分没有被渲染,这是我的主要问题!
【问题讨论】:
-
您需要在视图中将表单作为响应上下文的一部分传递。请参阅文档中的此示例:docs.djangoproject.com/en/1.10/topics/forms/#the-view
标签: html django python-2.7 django-forms