Django 1.10没有显示db的帖子

时间:2016-12-20 16:25:13

标签: python django python-3.x

我正在学习django框架写博客。我在django-admin中添加了一些帖子,我想在网上显示它们。

MODELS.PY

from django.db import models
from django.utils import timezone
from django.core.urlresolvers import reverse


class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager,
                     self).get_queryset()\
                    .filter(status='published')


class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Roboczy'),
        ('publish', 'Opublikowany')
    )
    title = models.CharField(max_length=300)
    slug = models.SlugField(max_length=300, unique_for_date='publish')
    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=20,
                              choices=STATUS_CHOICES,
                              default='draft')
    object = models.Manager()
    published = PublishedManager()

    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.published.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})
BLOG APP

URLS.PY

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

urlpatterns = [

    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'),
]

URLS.PY (项目级别)

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/', include('blog.urls',
                         namespace='blog',
                         app_name='blog')),


]

list.html

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

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

{% block content %}
    <h1>Mój BLOG</h1>
    {% for post in posts %}
        <h2>
            <a href="{{ post.get_absolute_url }}">
                {{ post.title }}
            </a>
        </h2>
        <p>
            Opublikowane {{ post.publish }} przez {{ post.author }}
        </p>
        {{ post.body|truncatewords:30|linebreaks}}
    {% endfor %}
{% endblock %}

我不知道为什么这些帖子没有显示在list.html上。我认为错误在于get_absolute_url。

3 个答案:

答案 0 :(得分:1)

而不是

posts = Post.published.all()

尝试

posts = Post.objects.all()

答案 1 :(得分:0)

好的,我必须更改帖子= Post.objects.all()和list.htlm

<a href="{{ post.get_absolute_url }}">

<a href="{{ posts }}">

答案 2 :(得分:0)

Urlpatterns错了。

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

根据视图,url模式应为:

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

此外,观点错误:

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})

请注意,查询字段为 publish__year (不是publish_year),发布___月(不是publish_month)和 publish__date (不是publish_date)

进行这些更改后,get_absolute_url也可以正常工作。 我不知道您是否要使用Post.objects.all()Post.published.all()。理想情况下,它应该是Post.published.all(),因为通常只显示已发布的帖子。