我有這樣的代碼(不給我預期的結果)分配變量子模板中{%包含%}標籤的Django
#subject_content.html
{% block main-menu %}
{% include "subject_base.html" %}
{% endblock %}
#subject_base.html
....
....
<div id="homework" class="tab-section">
<h2>Homework</h2>
{% include "subject_file_upload.html" %}
</div>
子模板:
#subject_file_upload.html
<form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="submit">
</form>
和我的看法
#views.py
@login_required
def subject(request,username, subject):
if request.method == "POST":
form = CarsForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect("/")
form = CarsForm()
return render_to_response('subject_content.html', {'form':form}, context_instance=RequestContext(request))
上面的代碼以我想要的方式創建HTML,但表單不更新數據庫。
,但
如果我跳過中間的模板,並直接進入上傳的形式,它工作正常:
#subject_content.html
{% block main-menu %}
{% include "subject_file_upload.html" %}
{% endblock %}
請幫助我,使之與中間模板的工作。 我想這樣做,因爲我不想多次輸入相同的代碼。
你是否嘗試過用'with'使用'include'? – Besnik 2012-07-24 21:25:22
@Besnik謝謝!!!!有用!!! – Vor 2012-07-24 21:34:37