【问题标题】:Form not Rendered in Django表单未在 Django 中呈现
【发布时间】: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 }} 部分没有被渲染,这是我的主要问题!

【问题讨论】:

标签: html django python-2.7 django-forms


【解决方案1】:

您的索引应该是这样的,您需要将表单传递给模板上下文。

def index(request):
 form = BForm()
 context = {
  'form': form,
 }
 return render(request, 'front/index.html', context)

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多