2017-12-07 12 views
0

ページのヘッダーに定義されたモデルがページに表示され、メインページの読み込みと更新が行われますが、別のビューに移動するとモデルは表示されません。ヘッダーの残りの部分はそこにあり、ダイナミックなデータは存在しません。ヘッダーのモデルは、ヘッダーを使用するビューに表示されません。

どこが間違っていますか? 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 %} 
+1

をあなたは詳細のために返されるショートカットをレンダリングし、他のビュー機能にモデルを渡していることを確認していますか? – SciGuyMcQ

+0

post_detailをレンダリングするビューはどこにありますか?ヘッダーのデータを渡していますか? –

+0

@SciGuyMcQああ、それは修正だった、はい。私は "propagandas = Propaganda.objects.all()。order_by( '?')[:1]"をviews.pyのpost_detailに追加し、また 'propagandas':propagandasを繰り返しました。私はdjangoについて学ぶことがたくさんある。あなたが答えとしてあなたのコメントを投稿するなら、私はそれを受け入れるでしょう。 @ダニエル - はい、それは重要ではないと思っていませんが、重要でした! –

答えて

1

まず:なぜあなたはpost_detailの見解を示していません。それは動作しないものではありませんか?

2番目:基本テンプレートを拡張している場合でも、必要な動的データを生成するために適切なコンテキストを渡す必要があります。

私はそれが役に立ちそうです。

+0

はい!ありがとうございました!みんなにはっきりと分かっていたことは私には分かりませんでした。 –

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とレンそれにこだわる、あなたはそこに着くよ:)

関連する問題