无法在我的列表中查看我的帖子.html

时间:2017-11-14 15:00:21

标签: python django django-models django-templates django-views

我创建了一个博客 这是我的models.py配置:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
# Create your models here.
class PublishedManager(models.Manager):

    def get_queryset(self):
        return super(PublishedManager, self).get_queryset()\
                .filter(status='published')
class post(models.Model):
    STATUS_CHOICE=(
        ('draft','DRAFT'),
        ('published','Published'),
        ('admin','admin'),
    )

    title=models.CharField(max_length=250,null=True)
    author=models.ForeignKey(User,related_name='blog_posts',null=True)
    slug=models.SlugField(max_length=250,unique_for_date='publish',null=True)
    body=models.TextField(default='')
    publish=models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True,null=True)
    updated =models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,
                            choices =STATUS_CHOICE,
                                default='draft')
    objects = 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('%dz'),
                                                self.slug])

这是我的views.py:

from django.shortcuts import render
from .models import post
# Create your views here.
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/index.html',{'post':post})

这是我的base.html:

{%load staticfiles%}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>{% block title %}
      <link rel="stylesheet" href={% static "/css/blog.css"%}>
      {% endblock %}
    </title>
  </head>
  <body>
<div id="content">
  {% block content %}
  {% endblock %}


</div>
<div id="sidebar">
  <h2>my blog</h2>
  <h3>this blog</h3>

</div>
  </body>
</html>

和我的list.html文件:

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

{% block title  %}My blog{% endblock %}
{% block content %}



  <h1>My 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 %}

这是我的urls.py(app文件夹)

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})/(?P<post>[-\W]+)/$',
    views.post_detail,
    name='post_detail'),
]

这是我的项目文件夹的urls.py文件:

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

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

并且我使用管理员用户添加了一些帖子对象但是当我尝试访问该页面时,根据教程的预期输出如下所示: enter image description here

但我的输出是: enter image description here

1 个答案:

答案 0 :(得分:2)

首先,上下文字典中的键应该是字符串'posts',而不是变量posts,正如@ABDUL在评论中指出的那样。

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

然后您需要修复NoReverseMatch错误。在get_absolute_url方法中,您有%dz。它应该是'%d'。删除z

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