Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 字段名和帮助文本在django中以多种形式消失_Python_Django_Django Models_Django Forms_Django Views - Fatal编程技术网

Python 字段名和帮助文本在django中以多种形式消失

Python 字段名和帮助文本在django中以多种形式消失,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,我有多个表单,可以处理一个视图。 当我想在index.html中显示我的表单并指定字段时,例如{{form_1.some_field},所有帮助文本和字段名称都会消失 当我使用{form_1}}时,一切都正常运行。有什么问题 这是我的文件: index.html Views.py {{form}}调用form.\uu str\uu方法,另一方面调用form.as\u表。因此,由于{{form}}和{{form.as_table}以相同的方式呈现 Form类还支持不同类型的呈现方法,如as_tab

我有多个表单,可以处理一个视图。 当我想在index.html中显示我的表单并指定字段时,例如{{form_1.some_field},所有帮助文本和字段名称都会消失

当我使用{form_1}}时,一切都正常运行。有什么问题

这是我的文件:

index.html Views.py {{form}}调用form.\uu str\uu方法,另一方面调用form.as\u表。因此,由于{{form}}和{{form.as_table}以相同的方式呈现

Form类还支持不同类型的呈现方法,如as_table、as_p、as_ul这就是表单对象呈现为html的方式。所有这些方法的实现都在BaseForm类中,它表示表单的父类。这是

所以,你应该这样做:


若您试图像以前一样手动呈现表单字段,那个么您也应该手动呈现帮助文本,它表示字段的属性。

您到底是什么意思?什么是消失?标签?我的意思是,index.html中没有显示我的模型的详细名称@gachdavitverbose_名称呈现为html标签。在您的例子中,{form_1.some_field.label}和verbose_name将显示在模板中。是的,它会有所帮助。谢谢但是你知道原因是什么吗@gachdavitYes。{{form}调用form.\uu str\uuuuuuuuu方法,另一方面调用form.as\u table{{{form.as\u table}。Form类还支持不同类型的呈现方法,如as_table、as_p、as_ul这正是Form对象应该如何表示为html的方式。所有这些方法的实现都在BaseForm类中,它表示“Form”的父类。这是源代码。[啊哈…我会在你的回答中添加你的评论@gachdavit@mostafarahnama我在评论中也回答了你关于详细名字的问题。祝你好运!
<form method="post" class="mos-rtl">
    {% csrf_token %}
    <div>
        <h4 class="mos-rtl">Section 1</h4>
        <p>{{ form_1.some_field }}</p>              
    </div>
    <div>
        <h4 class="mos-rtl">Section 2</h4>
            {{ form_2.some_field }}
        <button type="submit" >submit</button>
    </div>
</form>
class Form1(ModelForm):
        class Meta:
            model = Model1
            fields = '__all__'
class Form2(ModelForm):
    class Meta:
        model = Model2
        fields = '__all__'
def my_view(request):
    if request.method == "POST":
        form_1 = Form1(request.POST)
        form_2 = Form2(request.POST)
        if form_1.is_valid() and form_2.is_valid():
            new_record_1 = form_1.save(commit=False)
            new_record_1.save()
            new_record_2 = form_2.save(commit=False)
            new_record_2.record_1 = new_record_1
            new_record_2.save()
            return redirect('administrator:view_admin_curriculum')

    else:
        form_1 = Form1(request.POST)
        form_2 = Form2(request.POST)
    template = 'index.html'
    context = {'form_1': form_1, 'form_2': form_2}
    return render(request, template, context)
<form method="post" class="mos-rtl">
    {% csrf_token %}
    <div>
        <h4 class="mos-rtl">Section 1</h4>
        <p>{{ form_1.some_field }} {{ form_1.some_field.help_text }}</p>              
    </div>
    <div>
        <h4 class="mos-rtl">Section 2</h4>
            {{ form_2.some_field }} {{ form_2.some_field.help_text }}
        <button type="submit" >submit</button>
    </div>
</form>