user
user

Reputation: 317

List of posts not displaying - django

I have created a post model and would like to view the posts in post_list. While creating the new post, it is redirecting to post_list but not displaying any post. Also, in my post_form I have rendered the fields manually by using django templates. I couldnt figure out where I have made the mistake. Can someone please help me out. Thanks

models.py

class Post(models.Model):

    author              =       models.ForeignKey(User, on_delete = models.CASCADE)
    slug                =       models.SlugField(unique=True, blank=True, default=uuid.uuid1)

Upvotes: 1

Views: 928

Answers (2)

Ralf
Ralf

Reputation: 16485

You probably need to use lowercase variable name in your post_list.html.

For instance, {{ Post.title }} should probably be lowercase {{ post.title }}. There are a few places to change that.

Upvotes: 0

Lemayzeur
Lemayzeur

Reputation: 8525

By default the context_object_name is object_list

Either you access your Posts list in template with object_list

{% for post in object_list %}
     {{ post }} <!-- with lowercase -->
{% endfor %}

Or you change the context_object_name to post_list, so that way you will be able to access the post list with post_list in template

class PostListView(ListView):
     model = Post
     context_object_name = 'post_list'

Upvotes: 1

Related Questions