【发布时间】:2017-06-11 09:01:40
【问题描述】:
使用 Django 1.10,我正在构建一个博客/投资组合混合网站。除了 Mysite,我还有两个应用程序,Blog 和 Portfolio。在 blog/templates/ 中,我有一个 index.html,它显示来自 Blog 和 Portfolio 的内容(15 个最近的博客文章和 5 个最近的投资组合项目)。
所以 index.html 页面看起来像这样:
如您所见,数据未出现。但是,当我导航到博客页面和投资组合页面时,它会发生。例如博客页面:
我认为这个问题与我正在进行的多级扩展有关。因为 Blog 和 Portfolio 页面都显示来自数据库的内容,这让我觉得模型没问题,但视图有问题。 index.html 扩展了 base_generic.html 模板,而我的 recent_blog_posts.html 和 recent_portfolio_pieces.html 扩展了 index.html。
我不确定如何解决此问题。有什么建议我做错了吗?
项目结构
mysite/
---blog/
------static/
---------css/
---------images/
------------blogpostimages/
------------favicon.ico
------templates/
---------blog/
------------blog_post.html
------------blog_list.html
------------recent_blog_posts.html
---------base_generic.html
---------index.html
---------bio.html
---------resume.html
------admin.py
------apps.py
------models.py
------tests.py
------urls.py
------views.py
---portfolio/
------static/
---------css/
---------images/
------------portfoliopieceimages/
------templates/
---------portfolio/
------------portfolio_piece.html
------------portfolio_list.html
------------recent_portfolio_pieces.html
------admin.py
------apps.py
------models.py
------tests.py
------urls.py
------views.py
---mysite/
------settings.py
------urls.py
------wsgi.py
manage.py
db.sqlite3
requirements.txt
blog/views.py
from django.shortcuts import render
from django.views import generic
# Create your views here.
from .models import Blog
def index(request):
"""
View function for home page of site.
"""
# Generate most recent blog post
title = Blog.objects.all()
post_date = Blog.objects.all()
get_absolute_url = Blog.objects.all()
# Render the HTML template index.html with the data in the context variable.
return render(
request,
'index.html',
context={'title': title,
'post_date': post_date,
'get_absolute_url': get_absolute_url,
}
)
def recent_blog_posts.html(request):
blog = Blog.objects.order_by('-post_date')[0:11]
return render(request, 'index.html', {'blog': blog})
class BlogListView(generic.ListView):
"""
Generic class-based view for a list of blog posts.
"""
model = Blog
paginate_by = 20
def get_queryset(self):
return Blog.objects.order_by('-post_date')
class BlogDetailView(generic.DetailView):
"""
Generic class-based detail view for a blog post.
"""
model = Blog
def bio(request):
return render(
request, 'bio.html'
)
def resume(request):
return render(
request, 'resume.html'
)
index.html
<div>
<h2>Recent Blog Posts</h2>
<div>
{% block blogs %}
{% if blog_list %}
{% for blog in blog_list %}
<article>
<header>
<h4><small>{{blog.post_date}} » </small><a href="{{ blog.get_absolute_url }}">{{ blog.title }}</a></h4>
</header>
</article>
{% endfor %}
{% else %}
<p>Unfortunately, there are no blog posts yet.</p>
{% endif %}
</div>
{% endblock %}
</div>
<div>
<h2>Portfolio</h2>
{% if portfolio %}
{% for portfolio in portfolio %}
<div class="col-xs-12 col-sm-6 thumb">
<a class="thumbnail" href="{{ portfolio.get_absolute_url }}">
{% load static %}
<img class="img-responsive" src="{{ portfolio.cover_image }}" alt="">
<p>{{ portfolio.title }}</p>
<p>{{ portfolio.client_name }}</p>
</a>
</div>
{% endfor %}
{% else %}
<div>
<p>Unfortunately, there are no portfolio pieces yet.</p>
</div>
{% endif %}
【问题讨论】:
-
在您的模板中,您使用的是
blog_list上下文变量。但是在您的索引视图中,上下文返回不包含此变量。您有 title、post_date 和 get_absolute_url。此模板中的变量portofolio也是如此。 PS:请修复代码缩进 -
@Wilfried 谢谢!这行得通,如果你把它变成一个答案,我会选择它
-
完成。很高兴能帮助你。并且要严谨。这是优秀开发人员的资产;)
标签: django django-templates django-views