Django多级扩展未出现

Django多级扩展未出现,django,django-templates,django-views,Django,Django Templates,Django Views,使用Django 1.10,我正在构建一个博客/投资组合混合网站。除了Mysite,我还有两个应用程序,Blog和Portfolio。在blog/templates/I中有一个index.html,它可以显示blog和公文包中的内容(15篇最新的博客文章和5个最新的公文包项目) 因此index.html页面如下所示: 如您所见,数据未出现。但是,当我导航到博客页面和公文包页面时,它会这样做。例如,博客页面: 我认为这个问题与我正在进行的多层次扩展有关。因为博客和公文包页面都显示来自数据库的内

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

因此index.html页面如下所示:

如您所见,数据未出现。但是,当我导航到博客页面和公文包页面时,它会这样做。例如,博客页面:

我认为这个问题与我正在进行的多层次扩展有关。因为博客和公文包页面都显示来自数据库的内容,这让我觉得模型还可以,但视图有些问题。index.html扩展了base_generic.html模板,而我最近的_blog_posts.html和最近的_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'
)
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

<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 %}

最近的博客文章
{%block blogs%}
{%if blog_list%}
{blog_list%}中的blog为%
{%endfor%}
{%else%}
不幸的是,目前还没有博客帖子

{%endif%} {%endblock%} 文件夹 {%if公文包%} {投资组合%中的投资组合为%1} {%endfor%} {%else%} 不幸的是,目前还没有投资组合

{%endif%}
您必须在模板中使用变量pass to您的上下文。 因此,您必须将变量
blog\u list
传递到您的上下文中,以便在模板中对其进行迭代

以下示例:(仅适用于
博客列表


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

# Generate most recent blog post

title = Blog.objects.all()
post_date = Blog.objects.all()
get_absolute_url = Blog.objects.all()
Blog.objects.all()返回包含所有博客实例的查询集,而不是单个博客实例的标题/发布日期/获取绝对url

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

{% for portfolio in portfolio %}
不要使用与您迭代的变量相同的变量名,将后者更改为portfolio或portfolio_list

要使其工作,您可以尝试以下方法:

index.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'
)
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 %}


在模板中,您使用的是
blog\u列表
上下文变量。但在索引视图中,上下文返回不包含此变量。您有标题、发布日期和获取绝对url。此模板中的变量
portofolio
也是如此。PS:修复代码的缩进pls@Wilfried非常感谢。这是有效的,如果你把它变成一个答案,我会选择它完成。很高兴帮助你。而且要严格。它是优秀开发人员的资产;)嗯,我明白你的意思了。我只是想实施你的建议,但我想在它起作用之前我需要做一些db调整。您知道为什么使用与我迭代的变量相同的变量名是不好的吗?(我一般不熟悉Django和Python)我不确定它在模板中是如何工作的,但在Python代码中,它会将portfolio变量的值更改为循环结束后最后一次迭代的值。