将variables分配给{%include%}标签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))
上面的代码以我想要的方式创buildHTML,但是表单不更新数据库。
但,
如果我跳过中间模板并直接进入上传表单,则工作正常:
#subject_content.html {% block main-menu %} {% include "subject_file_upload.html" %} {% endblock %}
请帮助我使它与中间模板一起工作。 我想这样做,因为我不想多次input相同的代码。
就像@Besnikbuild议的那样,这很简单:
{% include "subject_file_upload.html" with form=form foo=bar %}
include
的文档提到了这一点。 它还提到,只能使用给定的variables来渲染模板,而不能inheritance任何其他variables。
谢谢@Besnik