【问题标题】:Django: html not displaying form using block contentDjango:html不使用块内容显示表单
【发布时间】:2020-04-03 07:14:03
【问题描述】:

enter image description here

//form.html

{% load crispy_forms_tags %}

{% block content %}
<form {% if form_id %} id='{{ form_id }}' {% endif %} class='form' method='POST' action="{% if action_url %}{{ action_url }}{% endif %}">
    {% csrf_token %}
    {{ form|crispy }}
    <input class="btn btn-primary" type="submit" value="{{ btn_title }}" />
</form>
{% endblock %}
//list_view.html
{% extends "base.html" %}

{% block content %}
    <div class="row">
        <div class='col-sm-3 col-xs-12' style='background-color:red;'>
        <h1>{{ request.user }}</h1>
        </div>
        <div class='col-sm-9 '>
            {% if not request.GET.q %}
                <div class="">
                    {% include "tweets/form.html" with form=create_form action_url=create_url
                    btn_title='Tweet' form_id='tweet-form' %}
                </div>
                <hr>
            {% endif %}

            <div id="tweet-container">
                
            </div>

        </div>
    </div>
    
{% endblock content %}

由于某种原因,当我尝试在 list_view.html 的块内容中使用 form.html 时,它只返回 {% include "tweets/form.html" with form=create_form action_url=create_url btn_title='Tweet' form_id='tweet-form' %} 正如你在我发布的照片​​中看到的那样。我试图使它成为一个带有提交按钮的文本区域。有没有人有什么建议

【问题讨论】:

    标签: html django django-rest-framework django-templates


    【解决方案1】:

    我被这个问题困住了,找到了答案。很简单:

    {% include "tweets/form.html" with form=create_form action_url=create_url btn_title='Tweet' form_id='tweet-form' %}
    

    以上行必须在单行上。不要拆分它!

    【讨论】:

      【解决方案2】:

      如果您想使用包含标签包含另一个 html 文件,则不必在该特定文件上使用块内容和结束块标签。在要包含 html 的文件上,只需在块内容中使用此标记即可。

       {% include 'form.html' %}
      

      【讨论】:

        猜你喜欢
        • 2015-10-18
        • 1970-01-01
        • 2013-03-09
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多