标头中的模型不会出现在使用标头的视图中

时间:2017-12-07 20:20:25

标签: django python-3.x django-models

我页面标题中定义的模型显示在主页面上的页面加载和刷新上,但是当我导航到另一个视图时,模型不会出现在那里。标题的其余部分就在那里,只有动态数据不存在。

我哪里错了? Python和Django都是新手,所以如果有人可以提供帮助,请不要害怕光顾和Eli5:)

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

了header.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Christopher's weblog</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Blog site.">

        <!-- Let's get the static files where we use them, but this could be before the html tag -->
        {% load staticfiles %}
        <link rel="stylesheet" href="{% static 'blog/css/bulma.css' %}" type="text/css"/>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
        <!-- <link rel="stylesheet" href="{% static 'blog/css/cupar.css' %}" type="text/css" /> -->
    </head>    
    <body>

        <section class="hero is-success is-bold">
            <div class="hero-body">
                <div class="container">
                    {% block slogan %}
                        {% for propaganda in propagandas %}
                            <h6>{{ propaganda.slogan }} it's</h6>
                        {% endfor %}
                    {% endblock %}
                    <h1 class="title" style="display: inline-block;">
                        <span class="icon">
                            <i class="fa fa-home"></i>
                            </span>
                        <a href="{% url 'post_list' %}">The Guide</a>
                    </h1>

                    {% if user.is_authenticated %}
                    <div class="field">
                        <div class="control">                            
                            <a href="{% url 'post_new' %}" class="button is-warning is-pulled-right">
                                <span class="icon">
                                    <i class="fa fa-plus-square"></i>
                                </span>
                                <span>New post</span>
                            </a>
                        </div>
                    </div>
                    {% endif %}

                </div><!-- @container -->
            </div>
        </section>
        <!-- content under header in here-> -->
        {% block content %}
        {% endblock %}
    </body>
</html>

views.py

from django.template import RequestContext
from django.shortcuts import render, get_object_or_404, redirect
from .models import Post, Propaganda
from .forms import PostForm
from django.utils import timezone

# Create your views here.
def post_list(request):
    posts = Post.objects.all()
    propagandas = Propaganda.objects.all().order_by('?')[:1]
    return render(request, 'blog/post_list.html', {'posts': posts, 'propagandas': propagandas})

post_detail.html(模型未显示):

{% extends 'blog/header.html' %}

{% block content  %}
some html / form that has nothing to do with the header.
{% endblock %}

post_list.html(模型工作正常的“索引”页面)

{% extends "blog/header.html" %}

{% block content %}

    {% for post in posts %}
        <section class="section">
        more html 
{% endblock %}

2 个答案:

答案 0 :(得分:1)

首先:为什么不显示post_detail的视图。不是那个不起作用的吗?

第二:即使您要扩展基本模板,仍然必须传递适当的上下文才能生成所需的动态数据。

我希望它有所帮助。

答案 1 :(得分:1)

需要传递每个模板中扩展(重新使用)标题中所需的每个视图函数的数据。

def post_detil(request, id):
    posts = Post.objects.all()
    propagandas = Propaganda.objects.all().order_by('?')[:1]

    # ... add in other stuff relevant to post details ...
    post = Post.objects.get(pk=id)

    return render(request, 'blog/post_detail.html', {'posts': posts, 'propagandas': propagandas, 'post': post})

坚持使用@Rin和Len,你会到达那里:)

相关问题