【问题标题】:Django variable not visible in header sectionDjango 变量在标题部分不可见
【发布时间】:2017-12-31 22:05:59
【问题描述】:

我有一个博客的基本开始,它列出了从 header.html 扩展而来的 post_list.html 数据库中的博客文章。我也试图在标题中获取一个变量作为口号,但这不起作用。

header.html - 呈现时没有从管理页面输入并包含内容的“propaganda.slogan”:

<!DOCTYPE html>
  <html lang="en">
  <head>
    <title>hey!</title>
    <meta charset="utf-8" />
    {% load staticfiles %}
    <link rel="stylesheet" href="{% static 'blog/css/bulma.css' %}"      type="text/css"/> 
  </head>    
  <body>
    <section class="hero is-success is-bold">
        <div class="hero-body">
            <div class="container">
                {% block slogan %}
                    <ul>
                    {% for propaganda in propagandas %}
                        <li>{{ propaganda.slogan }}</li>
                    {% endfor %}
                    </ul>       
                {% endblock %}
                <h1 class="title">
                    My weblog
                </h1>
            </div>
        </div>
    </section>
    {% block content %}
    {% endblock %}
</body>
</html>

post_list.html 扩展了 header.html 并显示来自 models.py 的帖子列表:

{% extends "blog/header.html" %}
{% block content %}
    {% for post in posts %}
        <section class="section">
            <div class="container">
                <h1 class="title"><a href="#">{{ post.title }}</a></h1>
                <p>{{ post.summary|linebreaksbr }}</p>
                <p>published: {{ post.last_edited }}</p>
            </div>
        </section>
    {% endfor %}    
{% endblock %}

models.py 看起来像这样:

from django.db import models
from django.utils import timezone

# Create your models here.
class Propaganda(models.Model):
    slogan = models.CharField(max_length=140, blank=True, null=True)

    def __str__(self):
        return self.slogan


class Post(models.Model):
    title = models.CharField(max_length=140, blank=False, null=False)
    content = models.TextField()
    summary = models.CharField(max_length=500)
    created_date = models.DateTimeField()
    last_edited = models.DateTimeField()

    def __str__(self):
        return self.title

最后,views.py 是:

from django.shortcuts import render
from .models import Post, Propaganda

# Create your views here.
def post_list(request):
    posts = Post.objects.all()
    return render(request, 'blog/post_list.html', {'posts': posts})

def header(request):
    propagandas = Propaganda.objects.all()
    return render(request, 'blog/header.html', {'propagandas': propagandas})

那么为什么我在post_list.html中可以得到帖子标题、摘要和日期的列表,但在标题中却无法得到宣传标语的列表?

在我看来,几乎是相同的代码?

我从开发服务器或浏览器中没有收到任何错误:(

【问题讨论】:

    标签: python django python-3.5


    【解决方案1】:

    您需要将宣传列表传递给当前模板的上下文;包含模板与包含模板的 关联(如果这首先有意义的话)视图几乎没有关系:

    def post_list(request):
        posts = Post.objects.all()
        propagandas = Propaganda.objects.all()
        return render(request, 'blog/post_list.html', {'posts': posts, 'propagandas': propagandas})
    

    【讨论】:

    • 谢谢,这有效,当我在标题“header.html”中获取变量值时,你能解释一下为什么当前模板是“post_list”吗?我会认为模板是标题模板。似乎我有一个在标题之后和下方启动的 post_list 视图,但随后我将标语添加到该视图中,它们出现在标题中!对我来说没有意义,尽管您很快就发现了错误。好像我正在构建一个作为一个文件的视图,并在 views.py 中创建一个视图允许我在标题中获取对象?
    • include: 加载模板并使用当前上下文呈现它。当前上下文由post_list视图提供现在,不提供宣传清单。不要认为包含的模板具有单独的视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2022-09-23
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多