django cookiecutter extending base.html wipes out my web page

£可爱£侵袭症+ 提交于 2019-12-11 15:24:59

问题


I am working on a project that I started in June 2017 with the cookiecutter I had just installed. At the time, with respect to django, I was an absolute beginner. (I am a bit more advanced by now, but just a bit.)

Cookiecutter put a base.html in the templates directory (one level above the app subdirectories).

For a list of model rows, I have a template that works all by itself, as follows:

{% if brand_list %}
    <ul>
    {% for brand in brand_list %}
        <li><a href="/brands/{{ brand.id }}/">{{ brand.cTitle }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No brands are available.</p>
{% endif %}

But, if I put this at the top, I do not get the list::

{% extends "base.html" %}

What I get instead is the project root webpage, the one at /.

Is this base.html the problem, or something else?


回答1:


Your base.html mus have a pair of template tags like this:

{% block content %}{% endblock %}

The template that inherits from base.html populates the content between those tags:

So in your inherited template you put

{% extends "base.html" %}

{% block content %} 

    {% if brand_list %}
        <ul>
        {% for brand in brand_list %}
            <li><a href="/brands/{{ brand.id }}/">{{ brand.cTitle }}</a></li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No brands are available.</p>
    {% endif %}

{% endblock %}


来源:https://stackoverflow.com/questions/47964402/django-cookiecutter-extending-base-html-wipes-out-my-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!