2017-02-22 6 views
1

私はDjangoの新機能で、この本のブログチュートリアル「Django by Example」に従っています。Djangoのブログの投稿は返されません

以下のコードは、ステータスが「公開」のすべてのブログ投稿を返しますが、動作を拒否します。 list.htmlコードを使用して正しいページが表示されますが、表示される投稿はありません。私は二重チェックして、私はステータスが "公開"に設定されている管理サイトで投稿を作成しています。 I私はここですべてを含めていますので、問題はテンプレート、モデル、ビューまたはURLにあるのかわからない:

Models.py:

from django.db import models 
from django.utils import timezone 
from django.contrib.auth.models import User 
from django.template.defaultfilters import slugify 
from django.core.urlresolvers import reverse 

#Data model class for creating posts table enrties in database 

#Custom manager. Limits returned querysets to ones with published status 
class PublishedManager(models.Manager): 
    def get_queryset(self): 
     return super(PublishedManager, self).get_queryset()\ 
    .filter(status='published') #super used to call get_queryset method from parent class as it is currently being 
           #overridden here. 

class Post(models.Model): 
    STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'),) 
    title = models.CharField(max_length=250) 
    slug = models.SlugField(max_length=250, unique_for_date='publish') #A field for use in building urls 
    #slug = models.SlugField(slugify(title)) 
    author = models.ForeignKey(User, related_name='blog_posts') #Each post written by a user and a user can write many posts 
    body = models.TextField() 
    publish = models.DateTimeField(default=timezone.now) 
    created = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft') 

    objects = models.Manager() #The default manager 
    published = PublishedManager() #Custom manager 

    class Meta: 
     ordering = ('-publish',) 

    def __str__(self): 
     return self.title 

    def get_absolute_url(self): 
     return reverse('blog:post_detail', args=[self.publish.year, self.publish.strftime('%m'),self.publish.strftime('%d'), self.slug]) 

views.py:

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


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

def post_detail(request, year, month, day, post): 
    post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) 
    return render(request, 'blog/post/detail.html', {'post':post}) 

URLs.py

from django.conf.urls import url 
from . import views 

urlpatterns = [ 
    # post views 
    url(r'^$', views.post_list, name='post_list'), 
    url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'\ 
     r'(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'), 

テンプレートbase.html:

{% load staticfiles %} 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>{% block title %}{% endblock %}</title> 
     <link href="{% static "css/blog.css" %}" rel="stylesheet"> 

</head> 
<body> 
    <div id="content"> 
     {% block content %} 
     {% endblock %} 
    </div> 
    <div id="sidebar"> 
     <h2>My Site site</h2> 
      <p>This site is all about me</p> 
    </div> 
</body> 
</html> 

detail.html:あなたはあなたのカスタムマネージャを使用するPost.published.all()を使用する必要が

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

{% block title %}My Blog{% endblock %} 

{% block content %} 
    <h1>Blog</h1> 
    {% for post in posts %} 
    <h2> 
     <a href="{{ post.get_absolute_url }}"> 
      {{ post.title }} 
     </a> 
    </h2> 
    <p class="date">Published{{ post.publish }} by {{ post.author }}</p> 
    {{ post.body|truncatewords:30|linebreaks }} 
    {% endfor %} 
{% endblock %} 
+1

URLS.py、テンプレート、モデル、views.pyを別々のボックスに分けてください。どちらのファイルをどのファイルにするのは難しいです。 –

答えて

1

は、あなたの辞書にあなたがあなたのビューがあるとして小文字をしようとする場合があります資本Pとの「記事」を行っていますpostsを使用してください。それからそれのための値は可変のポストの代わりに文字列でした。

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

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

それはまたまだに行くのではありませんする必要がありますので、このようなものは、あなたがあなたのコンテキストオブジェクトデータとして文字列で渡している

def post_list(request): 
    posts = Post.published.all() 
    return render(request, 'blog/post/list.html', {'posts': posts}) 
+0

うわー。それは速かった。それをやっていたのは大文字だった。どうもありがとうございます。他のものは私が大嫌いだった。彼らは私が無知な混乱の中で彼らを変えた直前だった。 – Nick

+0

偉大な、それは助けてうれしい嬉しい問題はない!私は何かを把握しようとしているときに私はたくさんのことを動かす傾向があります。そして、それらを忘れてしまいます。 – davidejones

+0

@Nick働いたら答えを受け入れてください。 :) –

0

を動作するはずですあなたがビューにいるのでマネージャメソッドを呼び出していないので動作します。あなたは持っています:

posts = Post.objects.all() 

カスタマーマネージャーを使用する必要があります。

関連する問題