如何更改Django表单向导中模板的布局? [英] How to change the layout for templates in a Django Form-Wizard?

查看:220
本文介绍了如何更改Django表单向导中模板的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django的表单向导程序包来创建一个四步形式。
我已经为四个步骤中的每一个创建了表单。工作正常在我的情况下,我创建了4个单独的模板,表单向导的每个步骤都使用自己的模板。



但是我创建的每个模板基本相同。以下是每个代码。我从其他地方剪掉并粘贴。它有效,但我不完全明白如何或为什么:

 < form action =method =post > {%csrf_token%} 
< table>
{{wizard.management_form}}
{%如果wizard.form.forms%}
{{wizard.form.management_form}}
X
{%形式在wizard.form.forms%}
{{form}}
{%endfor%}
{%else%}
Y
{{wizard.form }}
{%endif%}
< / table>
{%如果wizard.steps.prev%}
< button name =wizard_goto_steptype =submitvalue ={{wizard.steps.prev}}> {%trans上一步%< / button>
{%endif%}
< input type =submitvalue ={%trans下一步%}/>
< / form>

我需要做的是改变第2步中第4个表单的布局。不要像现在这样将表单的输入元素堆叠在一起。我想通过操作来做一些不同的事情。



但是我不知道该怎么做。有人向我显示一个模板,他们使用的是实际的命名表单元素,而不是{{wizard.form}}?请记住,如果有验证错误,我仍然需要显示在此网页上。如何完成所有这一切?

解决方案

如果你想控制你的表单的渲染,你应该手动呈现表单域。您的表单毕竟仍然是标准的Django表单。







但是我每个模板创建的基本上是
相同。


您当然不应该为每个步骤重复相同的模板。 >

而不是使用您的模板作为基础模板更为简洁,并在您为要自定义布局的步骤提供的模板中扩展此基本模板。这样,您甚至可以轻松地为特定步骤嵌入额外的CSS或JS。



所以而不是

  {%for wizard in wizard.form.forms%} 
{{form}}
{%endfor%}

您将在基本模板中创建以下块

  {%block form%} 
{{wizard.form}}
{%endblock%}

这样,您可以通过简单地覆盖表单块来自定义表单的渲染。

  { %extendsbase_step.html%} 
{%block form%}
{{wizard.form.example_field_to_show}}
...
{%endblock%}


I am using Django's Form Wizard package to create a 4-step form. I have created forms for each of the four steps. It's working fine. In my case I have created 4 separate templates and each step of the form wizard uses its own template.

But each of these templates that I have created is substantially the same. Below is the code that is in each one. I cut and pasted this from elsewhere. It works, but I don't fully understand how or why:

<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
    {{ wizard.form.management_form }}
    X
    {% for form in wizard.form.forms %}
        {{ form }}
    {% endfor %}
{% else %}
    Y
    {{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "Previous Step" %}</button>
{% endif %}
<input type="submit" value="{% trans "Next Step" %}"/>
</form>

What I need to do is alter the layout for the form in step #2 of 4. I don't want the form's input elements to be stacked on top of each other as they currently are. I want do something a little different by manipulating the the .

But I don't know how to do this. Can someone show me a template in which they are using the actual named form elements instead of just {{wizard.form}}? Keep in mind, if there is a validation error, I still need that to show up on this webpage. How to accomplish all that? A simple example of what your template looks like would be ideal.

解决方案

If you want to control the rendering of your forms you should render the form fields manually. Your forms are still standard Django forms after all.


But each of these templates that I have created is substantially the same.

You certainly shouldn't repeat the same template for each step.

It would be cleaner to use your template as a base template instead and extend this base template in the templates you provide for the steps where you want to customize the layout. That way you could even easily embed extra CSS or JS for certain steps.

So instead of

{% for form in wizard.form.forms %}
  {{ form }}
{% endfor %}

you would create the following block in your base template

{% block form %}
  {{ wizard.form }}
{% endblock %}

That way you can customize the rendering of your forms for certain steps by simply overriding the form block.

{% extends "base_step.html" %}
{% block form %}
  {{ wizard.form.example_field_to_show }}
  ...
{% endblock %}

这篇关于如何更改Django表单向导中模板的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆