我的Django模板中有2个html页面。我试图在index.html中插入cats.html作为块,但没有任何反应。没有错误,没有显示。我已经查看了django文档和youtube。只是无法理解问题在哪里
的index.html:
{% load static %}
<!DOCTYPE doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link rel="stylesheet" type="text/css" href="{% static 'items/index-style.css' %}" />
<title>
my site
</title>
</head>
<body>
{% block cats %}
{% endblock cats %}
</body>
</html>
cats.html:
{% extends "index.html" %}
{% block cats %}
<div class="row column">
<p class="lead">
Категории товаров
</p>
</div>
<div class="row small-up-1 medium-up-2 large-up-3">
{% for category in categories %}
<div class="column">
<a href="/{{category.alias}}">
<div class="callout">
<p>
{{category.name}}
</p>
<p>
<img alt="image of a planet called Pegasi B" src="{{category.image}}"/>
</p>
<p class="lead">
<!-- Цена: {{tovar.price}} -->
</p>
<p class="subheader">
<!-- {{tovar.short_description}} -->
</p>
</div>
</a>
</div>
{% endfor %}
</div>
{% endblock cats %}
views.py:
from django.shortcuts import render
from django.http import HttpResponse, Http404
from django.template.loader import render_to_string
from items.models import *
# Create your views here.
def home(request):
try:
categories = Category.objects.all()
except:
raise Http404()
context = {
'categories':categories,
}
return render(request,"index.html",context)
答案 0 :(得分:1)
您将块名称与模板名称混淆。您不正如您所期望的那样将cats.html
插入index.html
,但您正在index.html
中展开cats.html
。您应该在视图中使用您的子模板(cats.html
),即将最后一行更改为:
return render(request, 'cats.html', context)
答案 1 :(得分:-1)
实际上问题是您使用index.html作为基本模板 然后继承你的cats.html 所以你必须渲染cat.html以获得所需的结果THIS IS HELPFUL FOR YOU
return render(request, 'cats.html', context)
谢谢:)