aba*_*sta 1 html django html5 templates python-3.x
目前,我有几乎两个完全相同的模板,他们使用相同的Django表单,但只有一个参数在这两种形式中发生变化,这就是动作方法,即
Django形式
class DropDownMenu(forms.Form):
week = forms.ChoiceField(choices=[(x,x) for x in range(1,53)]
year = forms.ChoiceField(choices=[(x,x) for x in range(2015,2030)]
Run Code Online (Sandbox Code Playgroud)
模板1
<form id="search_dates" method="POST" action="/tickets_per_day/">
<div class="row">
<div style="display:inline-block">
<h6>Select year</h6>
<select name="select_year">
<option value={{form.year}}></option>
</select>
</div>
<button type="submit">Search</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
模板2
<form id="search_dates" method="POST" action="/quantitative_analysis/">
<div class="row">
<div style="display:inline-block">
<h6>Select year</h6>
<select name="select_year">
<option value={{form.year}}></option>
</select>
</div>
<button type="submit">Search</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
唯一不同的是动作方法,所以我想知道是否可以重用一个仅在动作方法中有所不同的模板.如果有可能,你能帮我解决一下代码吗?
我检查了这个问题django - 如何重用几乎相同模型的模板?但是我没有在我的模板中使用任何模型.
当然有办法.{% include %}救援!
为表单创建一个基本模板,如下所示:
<!-- form_template.html -->
<form id="search_dates" method="POST" action="{{ action }}">
<div class="row">
<div style="display:inline-block">
<h6>Select year</h6>
<select name="select_year">
<option value={{form.year}}></option>
</select>
</div>
<button type="submit">Search</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
注意占位符action.我们将在下一步中使用它.
现在,您只需编写以下代码即可重用此模板:
<!-- a_template.html -->
{% include 'form_template.html' with action='/tickets_per_day/' %}
<!-- b_template.html -->
{% include 'form_template.html' with action='/quantitative_analysis/' %}
Run Code Online (Sandbox Code Playgroud)