Django多级扩展没有出现



使用Django 1.10,我正在构建一个博客/投资组合混合网站。除了Mysite,我还有两个应用程序,博客和公文包。在博客/templates/I中有一个index.html,它显示博客和公文包中的内容(15篇最新的博客文章和5个最新的公文包项目)。

所以index.html页面看起来是这样的:

https://i.stack.imgur.com/N7bEj.jpg

正如您所看到的,数据没有出现。然而,当我浏览到博客页面和公文包页面时,它确实会出现。例如,博客页面:

https://i.stack.imgur.com/iU7jl.jpg

我认为这个问题与我正在进行的多层扩展有关。因为博客和公文包页面都显示数据库中的内容,这让我认为模型还可以,但视图有问题。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传递到上下文,以便在模板中对其进行迭代。

以下示例:(仅适用于blog_list)

def index(request):
"""
View function for home page of site.
"""
blog_list  = Blog.objects.all()
# Render the HTML template index.html with the data in the context variable.
return render(request, 'index.html', context={'blog_list': blog_list})

您需要将数据传递到上下文中的模板。我真的不明白你想在索引中通过设置和传递上下文中的变量来做什么:

# Generate most recent blog post
title = Blog.objects.all()
post_date = Blog.objects.all()
get_absolute_url = Blog.objects.all()

Blog.objects.all()返回一个包含所有博客实例的查询集,而不是单个博客实例的title/post_date/get_absolute_url。

在模板中,您引用了两个上下文变量:blog_list和portfolio。您不会在索引中设置变量。我也会避免这样的说法:

{% for portfolio in portfolio %}

不要使用与您迭代的变量相同的变量名,将后者更改为portfolioportfolio_list

为了让它发挥作用,你可以试试这个:

索引.py

def index(request):
blog_list = Blog.objects.all()
portfolio_list = Portfolio.objects.all()
return render(request, 'index.html',
context={'blog_list': blog_list, 'portfolio_list': portfolio_list}
)

index.html文件更改中:

{% for portfolio in portfolio %}

{% for portfolio in portfolio_list %}

最新更新