user7671217
user7671217

Reputation:

cant see my post in my list .html

i created a blog this is my models.py configurations:

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

and this is my 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})

and this is my 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>

and my list.html file:

{% 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 %}

and this is my urls.py (app folder)

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

and this is my urls.py file of my project folder:

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

and i have added some post objects using admin user but when i tried to access the page the expected output according to the tutorial looks like this: enter image description here

but instead my output was : enter image description here

Upvotes: 0

Views: 50

Answers (1)

Alasdair
Alasdair

Reputation: 308769

Firstly, the key in your context dictionary should be the string 'posts', not the variable posts, as @ABDUL pointed out in the comments.

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

Then you need to fix the NoReverseMatch error. In your get_absolute_url method, you have %dz. It should be '%d'. Remove the 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])

Upvotes: 2

Related Questions