当无效数据以表格形式提供时,模板不存在

时间:2017-07-09 18:20:28

标签: django django-models django-templates django-views

在part_detail_view中,我将part_stock_form()传递给模板,以便显示在DetailView中添加库存的表单。

class part_detail_view(DetailView):
    model = part_list
    context_object_name = 'part_detail'
    template_name = 'part_detail.html'

    def get_context_data(self, **kwargs):
        context = super(part_detail_view, self).get_context_data(**kwargs)
        context['my_list'] = populate_nav_bar()
        context['form'] = part_stock_form()
        return context

股票的CreateView在下面给出

class stock_add_view(CreateView):
    model = part_stock
    fields = ['part_id','entry_date','supplier','amount','remaining']
    success_url = reverse_lazy('parts:part_list')

并且'part_detail.html'的模板是

<div>
{{ part_detail.part_id}}<br>
{{ part_detail.part_name }}<br>
{{ part_detail.cost }}<br>
{{ part_detail.available_quantity }}
</div>
<div >
<form method="post" action="{% url 'parts:stock_add_view'%}">
    {% csrf_token %}
          {{ form.errors }}
          {{ form }}
    <input type="submit">
</form>
</div>

通过stock_add_view()添加库存时,库存成功添加,但输入无效输入时,错误显示如下 error description

1 个答案:

答案 0 :(得分:1)

CreateView假设您拥有model_name_form形式的模板。在您的情况下,它是part_stock_form.html。来自docs

  

显示给GET请求的CreateView页面使用'_form'的template_name_suffix。例如,对于为示例作者模型创建对象的视图,将此属性更改为“_create_form”将导致默认template_name为“myapp / author_create_form.html”

因此,您需要创建名为 part_stock_form.html 的模板。如果您想使用 part_detail.html ,请尝试在视图中更改template_name变量(未经测试):

class stock_add_view(CreateView):
    model = part_stock
    fields = ['part_id','entry_date','supplier','amount','remaining']
    success_url = reverse_lazy('parts:part_list')

    template_name = 'part_detail.html'

希望有所帮助