问题:我一直无法在我的详细页面视图中显示内容,具体而言,此内容位于名为slider.html
和sidebar.html
的两个单独的html文件中,这些文件正在使用{ Django中的{1}}。
状态: 问题仍未解决,周四上次更新。 12月18日,上午11点。
{% include %}
和models.py
detailed.html
from django.views import generic
from . import models
from .models import FullArticle
# Create your views here.
class BlogIndex(generic.ListView):
queryset = models.FullArticle.objects.published()
template_name = "list.html"
randomArticle = FullArticle.objects.order_by('?').first()
class BlogDetail(generic.DetailView):
model = models.FullArticle
template_name = "detailed.html"
from django.db import models
from django.core.urlresolvers import reverse
# Create your models here.
class FullArticleQuerySet(models.QuerySet):
def published(self):
return self.filter(publish=True)
class FullArticle(models.Model):
title = models.CharField(max_length=150)
author = models.CharField(max_length=150)
slug = models.SlugField(max_length=200, unique=True)
pubDate = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
category = models.CharField(max_length=150)
heroImage = models.CharField(max_length=250, blank=True)
relatedImage = models.CharField(max_length=250, blank=True)
body = models.TextField()
publish = models.BooleanField(default=True)
gameRank = models.CharField(max_length=150, blank=True, null=True)
objects = FullArticleQuerySet.as_manager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("FullArticle_detailed", kwargs={"slug": self.slug})
def get_context_data(self, **kwargs):
context = super(BlogDetail, self).get_context_data(**kwargs)
context['object_list'] = models.FullArticle.objects.published()
return context
class Meta:
verbose_name = "Blog entry"
verbose_name_plural = "Blog Entries"
ordering = ["-pubDate"]
<!-- This grabs the sidebar snippet -->
{% include "sidebar.html" with object_list=object_list %}
<div class="mainContent clearfix">
<h1>{{object.title}}</h1>
<p class="date">{{object.pubDate|date:"l, F j, Y" }}</p> | <p class="author">{{object.author}}</p>
<img src="{{object.heroImage}}" alt="" class="largeImage">
<div class="contentBlock">
<img src="{{object.relatedImage}}" alt="" class="relatedImage">
<p class="content">{{object.body|linebreaks}}</p>
</div><!-- /.contentBlock -->
<!-- This grabs the slider snippet -->
{% include "slider.html" with object_list=object_list%}
</div><!-- /.mainContent -->
<div class="navContent">
{% for article in object_list|slice:":5" %}
<div class="navItem">
<div class="overlay">
</div><!-- /.overlay -->
<a href="{%url "detailed" slug=article.slug %}"><img src="{{article.relatedImage}}" alt="" class="navPicture"></a>
<a href="{%url "detailed" slug=article.slug %}"><p class="navTitle">{{ article.title|truncatewords:"4" }}</p></a>
</div><!-- /.navItem -->
{% endfor %}
</div><!-- /.navContent -->
答案 0 :(得分:1)
get_context_data
方法属于您的视图。如果你把它移到那里,你的侧边栏应该可以工作。
然而,通过使用上下文处理器可以更清晰地实现此目的。上下文处理器允许您为模板的所有提供某些数据,无论它们在何处。
在项目模块目录中创建context_processors.py
,并使其看起来像这样:
# myproject/myproject/context_processors.py
from myapp.models import FullArticle
def sidebar_articles(request):
"""Return the 5 most recent articles for the sidebar."""
articles = FullArticle.objects.all()[:5]
return {'SIDEBAR_ARTICLES': articles}
您必须在settings.py
中启用上下文处理器。通过添加TEMPLATE_CONTEXT_PROCESSORS
default setting并将新的上下文处理器添加到底部来执行此操作。像这样:
# settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"myproject.context_processors.sidebar_articles",
)
现在,您可以从任何模板中引用SIDEBAR_ARTICLES
。您的sidebar.html
可以改写为:
<!-- myapp/templates/detailed.html -->
<div>
{% for article in SIDEBAR_ARTICLES %}
<h1>{{ article.title }}</h1>
and so on ...
{% endfor %}
</div>
答案 1 :(得分:0)
我认为有必要将正确的参数传递给您所包含的模板。有关更多文档,请参阅https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include。
模板看起来像:{% include "sidebar.html" with article=object %}
更新1
在阅读了关于detailview的文档后,我的以下建议
(https://docs.djangoproject.com/en/1.7/ref/class-based-views/generic-display/#detailview)您的上下文似乎只包含object
。因此,我们必须将此传递给此包含的滑块模板。
{% include "slider.html" with article=object %}
侧边栏模板中的问题是您的上下文中没有object_list。您可以将其添加到DetailView中,如示例中所示。所以方法看起来像。
def get_context_data(self, **kwargs):
context = super(BlogDetail, self).get_context_data(**kwargs)
context['object_list'] = models.FullArticle.objects.published()
return context
模板的包含应该如下。
{% include "sidebar.html" with object_list=object_list %}
答案 2 :(得分:0)
注意您正确地将上下文传递给模板。但可能你必须在&#39; {{&#39;括号和变量名称。防爆。 {{article.title}}