made_in_india
made_in_india

Reputation: 2279

django inability to find blog url while rendering the html page

I am newbie to django. I am trying to develop blog app in django. I trying to render my fetched data into html but unable to do it for the following code for which it is printing no blog found even data is being fetched properly.

This is my views.py

def blogpost(request):

    latest_blog_list = BlogPost.objects.order_by('-pub_date')[:5]
    print "" + str(latest_blog_list)
    context = {'latest_poll_list' : latest_blog_list}
    print "" + str(context)

    #I can see the data is being fetched properly
    return render(request,'polls/blogPostlist.html',context)

my blogPostlist.html

    {% if latest_blog_list %}
    <ul>
    {% for blogpost in latest_blog_list %}
        <li> <a href="/blog/{{ blogpost.slug }}"> blogpost.title </li>
    {% endfor %}
    </ul>
    {% else %}
           <p> test No blog avilable</p>
    {% endif %}

models.py

    class BlogPost(models.Model):
        title       = models.CharField(max_length=255)
        description = models.CharField(max_length=255)
        post        = models.TextField()
        #media_file = models.ImageField(upload="")
        pub_date    = models.DateTimeField()
        visit_count = models.IntegerField(default=0)
        slug        = models.SlugField(unique=True, max_length=255)
        published   = models.BooleanField(default=True)
        tag         = models.ManyToManyField(BlogTag)
        catagory    = models.ManyToManyField(BlogCategory)


        def __unicode__(self):
            return u'%s' % self.slug

        class Meta:
            ordering = ["pub_date"]

Along with that if I want to get title and slug for the blog, how to do it?

Upvotes: 0

Views: 42

Answers (1)

mariodev
mariodev

Reputation: 15484

The context key name is wrong:

context = {'latest_poll_list' : latest_blog_list}

Instead it should be:

context = {'latest_blog_list' : latest_blog_list}

Upvotes: 2

Related Questions