简体   繁体   English

如何扩展Django CMS模板?

[英]How to extend Django CMS template?

I want to use a base template and then extend partial views. 我想使用基本模板,然后扩展部分视图。 extends not working as it is not showing base markup at all. extends无法正常工作,因为它根本没有显示基本标记。

base.html base.html

<div class="container">
    {% block content %}
    {% endblock %}
</div>

_hello.html _hello.html

    {% extends 'base.html' %}
   {% block content %}
    <div class="container">
        <div class="row">
            <div class="col-md-12 text-center"><h2>Survey About Computer Programming</h2></div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <h3>Programming</h3>
                <table class="table table-bordered table-striped table-responsive">
                    <thead>
                        <tr align="center">
                            <th>Main</th>
                            <th class="text-center">Option1</th>
                            <th class="text-center">Option2</th>
                            <th class="text-center">Option3</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>Do you love Programming?</td>
                            <td class="text-center"><input type="radio" class=""></td>
                            <td class="text-center"><input type="radio" class=""></td>
                            <td class="text-center"><input type="radio" class=""></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

    </div>
    <script src="/static/survey/js/plugin.js"></script>
 {% endblock %}

you have to put your code inside the block content . 您必须将代码放入块内容中 The extended template overwrites the blocks in the base template. 扩展模板将覆盖基本模板中的块。

<div class="container">
    {% block content %}
    {% endblock %}
</div>
    {% block js_bottom %}
    {% endblock %}

_hello.html _hello.html

{% extends 'base.html' %}
{% block content %}
<div class="row">
    <div class="col-md-12 text-center"><h2>Survey About Computer Programming</h2></div>
</div>
<div class="row">
    <div class="col-md-12">
        <h3>Programming</h3>
        <table class="table table-bordered table-striped table-responsive">
            <thead>
                <tr align="center">
                    <th>Main</th>
                    <th class="text-center">Option1</th>
                    <th class="text-center">Option2</th>
                    <th class="text-center">Option3</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Do you love Programming?</td>
                    <td class="text-center"><input type="radio" class=""></td>
                    <td class="text-center"><input type="radio" class=""></td>
                    <td class="text-center"><input type="radio" class=""></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
    {% endblock %}

{% block js_bottom %}
<script src="/static/survey/js/plugin.js"></script>
{% endblock %}

You Missed important thing whenver we are creating template like this we have to make sure every block like CSS, Content, JS must be defined proporly in base html file. 当我们创建这样的模板时,您必须错过重要的事情,我们必须确保必须在基本html文件中正确定义每个块(例如CSS,Content,JS)。 And when you extends this base template then you have to framed your content as per requirement. 并且,当您扩展此基本模板时,必须根据要求对内容进行框架设计。 Like if you need to put some content in the html page which extends the base html simply call as following {% block _block_name_should_be_here %} {% endblock %} 就像您需要在扩展基本html的html页面中放置一些内容一样,只需按照以下命令{%block _block_name_should_be_here%} {%endblock%}

Block name might be like css_part , content_part , js_part which needs to be used the html page. 块名称可能类似于css_part,content_part,js_part,它们需要在html页面中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM