【问题标题】:Django multi-level extends not appearingDjango多级扩展没有出现
【发布时间】:2017-06-11 09:01:40
【问题描述】:

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

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

http://imgur.com/y2UqBSS

如您所见,数据未出现。但是,当我导航到博客页面和投资组合页面时,它会发生。例如博客页面:

http://imgur.com/7P922Ga

我认为这个问题与我正在进行的多级扩展有关。因为 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


【解决方案1】:

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

# Generate most recent blog post

title = Blog.objects.all()
post_date = Blog.objects.all()
get_absolute_url = Blog.objects.all()

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

在模板中,您指的是两个上下文变量:blog_list 和投资组合。您没有在 index 中设置变量。我也会避免这样的声明:

{% for portfolio in portfolio %}

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

为了让它工作,你可以试试这个:

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

【讨论】:

  • 嗯,我明白你的意思了。我只是尝试实施您的建议,但我认为在它起作用之前我需要进行一些数据库调整。你知道为什么使用与我迭代的变量相同的变量名是不好的形式吗? (我是 Django 和 Python 的新手)
  • 我不确定它在模板中是如何工作的,但在 python 代码中它会将投资组合变量的值更改为循环结束后的最后一个迭代值。
【解决方案2】:

您必须在模板中将变量传递给您的上下文。 因此,您必须将变量 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})

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    相关资源
    最近更新 更多