django - 在表单中包含标签 django

标签 django django-forms django-templates

我尝试使用 django 在另一个模板中包含一个模板,如果我包含的模板没有变量,则一切正常,但是当模板有变量时,例如 {{form}} 不会渲染变量,但渲染模板:

浏览次数:

class IdentifiacionsUsuariosView(FormView):
    form_class  = FormProductores
    success_url = '/'
    template_name = 'propietarios.html'

    def form_valid(self,form):
        productor = form.save()
        return super(IdentifiacionsUsuariosView,self).form_valid(form)

模板-1.html

Hello    
<form action="." method="POST">{% csrf_token %}
    {{form}}
    <p><input type="submit" value="ok"></p>
    </form>

模板-2.html

<div class="span4" id="campo1">
{% include 'template-1.html'  %}
</div>

像这样,我看到“你好”和提交输入,但我无法渲染表单,我该怎么做?

最佳答案

https://docs.djangoproject.com/en/1.8/topics/forms/#reusable-form-templates

在代码中引用模板。

{% include "template-1.html" %}

If the form object passed to a template has a different name within the context, you can alias it using the with argument of the include tag:

{% include "form_snippet.html" with form=comment_form %}

关于django - 在表单中包含标签 django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666067/

相关文章:

python - 如何将 forloop.counter 连接到我的 django 模板中的字符串

python - Django View/Template 如何知道模型的最大值

javascript - 获取发送到 Python/Django 的 POST 请求的 None 值(通过 Axios 库)

生成 Django Postgres psycopg2 错误脚本 - 引用表名

python - 如何在 meezanine CMS 中向博客文章添加更多字段?

Django 日期格式 'dd-mm-yyyy'

python - 仅当外键存在时才保存内容的标准方法是什么?

django-models - Django - 在表单小部件类中获取实例 pk

python - AttributeError at/'tuple' 对象没有属性 '_meta'

django - 在多个 django 模板中渲染表单函数