我有一个基本模板,其中包含下拉菜单的另一个模板。如果我硬编码li标签的项目,它工作正常。如果我尝试传递objects.all(),它就不会填充。这是我的设置:
模型
class Category(models.Model):
name = models.CharField(max_length=40, blank=True)
def __unicode__(self): # Python 3: def __str__(self):
return self.name
查看
def dropdown_cats(request):
cats = Category.objects.all()
return render(request, 'home/dropdown-cats.html', {'cats': cats})
主/ base.html文件
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="{% static 'media/logo.png' %}">
<title>something.com</title>
<!-- Bootstrap core CSS -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="{% static 'dist/css/bootstrap.css' %}" type="text/css" media="screen" />
<link href="{% static 'dist/css/bootstrap.min.css' %}" rel="stylesheet" media="screen">
<!-- Custom CSS -->
<link href="{% static 'dist/css/custom.css' %}" rel="stylesheet" media="screen">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="site_wrapper" class="clearfix">
<!-- Above-Nav
================================================== -->
<div class="above-nav visible-desktop">
<div class="container" style="text-align:center;">
<a href={{home}}><img src="/static/media/webimages/frontpageheader.jpg" alt="something.com, click for home. "></a>
</div>
</div>
<!--END above-nav
================================================== -->
<!-- Navbar ==================================== -->
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href={{home}}>something.com</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
{% block navbar %}
{% endblock navbar %}
</ul>
</div>
</div><!--/.nav-collapse -->
</div><!-- navbar-inner -->
</div>
<!-- End Navbar =================================== -->
<!-- Start Grid layout ============================ -->
<!--<div class="container-fluid">-->
<div class="container-fluid">
<div class="row" style="text-align:center">
<div class="col-xs-3"><h2>My Most Recent Blogs</h2>
{% block left_side %}
{% endblock left_side %}
</div>
<div class="col-xs-6">
{% block middle %}
{% endblock middle %}
</div>
<div class="col-xs-3"><h2>Links</h2>
{% block right_side %}
{% endblock right_side %}
</div>
</div>
</div>
<!-- End Grid Layout ====================== -->
</div>
<!-- close the wrapper ====================== -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="{% static 'dist/js/bootstrap.min.js' %}"></script>
</body>
</html>
主/ home.html做为
{% extends "home/base.html" %}
{% block navbar %}
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Categories<span class="caret"></span></a>
{% include 'home/dropdown-cats.html' %}
</li>
<li><a href="#">Previous Blogs</a></li>
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% endblock navbar %}
{% block left_side %}
{% endblock left_side %}
{% block middle %}
{% endblock middle %}
{% block right_side %}
{% endblock right_side %}
主/下拉-cats.html
<ul class="dropdown-menu">
{% if cats %}
{% for cat in cats %}
<li><a href="{% url 'category' cat.id %}">{{ cat.name }}</a></li>
{% endfor %}
{% else %}
<li><a href="#">BAD TEST</a></li>
{% endif %}
</ul>
如果我通过用类似GOOD TEST之类的东西替换for循环中的url标记来测试它,那么仍然没有插入因为猫是空的。我得到的只是一个下拉条目“BAD TEST”。我已经使用shell来确保Category.objects.all()按类别返回列表。
不知道Post语句是如何在视图中得到的。一定是不小心贴了它。无论如何,我已将其从视图中排除,并包含我的所有html以获取更多详细信息。我想我明白你在说丹尼尔,但我有点困惑。这不是本教程如何修改模板,将其包含在渲染参数中:render(request,'template.html',dicts)
答案 0 :(得分:0)
这里没有真正显示足够的信息,但我怀疑你对模板和视图感到困惑。简单地包含另一个模板不会“调用”另一个视图:唯一调用视图的是URL处理程序。模板本身对视图一无所知。
如果您想为其他模板动态添加上下文,则需要将其设置为自定义inclusion tag。
答案 1 :(得分:-1)
将模板中的代码段替换为:
<ul class="dropdown-menu">
{% for cat in cats %}
<li><a href="{% url 'category' cat.id %}">{{ cat.name }}</a></li>
{% empty %}
<li><a href="#">BAD TEST</a></li>
{% endfor %}
</ul>
只要 View 代码中设置的cats
查询集包含1个或多个项目,上述内容肯定会有效。上面的模板代码中使用的for .. empty
模板标记记录为here。