【问题标题】:django form does not accessible from inherited templates无法从继承的模板访问 django 表单
【发布时间】:2014-07-21 21:37:01
【问题描述】:

我有一个 second-base.html,它是 user-image.html 的父级 当我加载 second-base.html 时,我将一些表单传递给它,所有表单都可以访问,并且一切正常。但是当我链接到 user-image.html 是 socond-base.html 的孩子时,我无法访问 forms 。

如果有人可以帮助我

这是我的观点.py

def dashboard(request):
context = RequestContext(request)
if request.method == 'GET':
    questioner_form = QuestionerForm()
    return render_to_response('second-level-base.html', locals(), context)

这是我的 second-base.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<title> {% block title %}{% endblock %}</title>
{% block head %} {% endblock head%}
</head>
<body>
 <div class="modal fade " id="questionerModal" tabindex="-1" role="dialog"      aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog ">
<div class="modal-content">
  <div class="modal-header" >
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>        
  </div>
  <div class="modal-body">
    <form class="form col-md-12 center-block" id="questionerForm"  role="form"   method="post" enctype="multipart/form-data" action="{% url 'Questioner' %}">
        {% csrf_token %}
       <div class="form-group">
        {{questioner_form.as_p}}           
       </div>
    </form>
   </div>

  </div>
 </div>

这是我固有的 userimage.html 的一部分,它需要访问传递给它的父级 (second-base.html) 的表单

 {% extends 'second-level-base.html' %}
 {% load static %}
 {% block title %} User Image {% endblock %}
 {% block body %}
 {{questioner_form.as_p}}
 {% endblock %}
 </body>
 </html>

【问题讨论】:

  • 请发布您的代码
  • 我添加了代码。如果你能帮助我。 tnx 很多
  • 如何渲染 userimage.html ? .. 它应该和我的 second-base.html 一样呈现
  • 你的意思是我应该在呈现 userimage.html 的函数中传递我的表单?是不是反对 DRY 。一旦我将表单传递给父模板,我就可以在它的子模板中使用它!!!???

标签: django forms inheritance django-templates


【解决方案1】:

https://docs.djangoproject.com/en/dev/topics/forms/#reusable-form-templates 可以看出,您必须使用include 才能使second-base.html 的模板对userimage.html 可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多