问题
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