Home > Software engineering >  I can't find out my mistake in displaying database data in django template
I can't find out my mistake in displaying database data in django template

Time:11-04

I am working on a Django project. I have to display database data in the Django template but I can't find out my mistake in my code that is not allowing me to display data on the HTML page.

views.py

def viewposts(request):
     posts = NewPost.objects.all()

     return render(request,"network/index.html",{
         "posts" : posts,
     })

Here is my model:

models.py

class NewPost(models.Model):
    user = models.ForeignKey(User, on_delete = models.CASCADE)
    post = models.TextField()
    timestamp = models.DateTimeField(auto_now_add = True)

    def __str__(self):
         return f"post : {post} || user: {user} || timestamp: {timestamp} "

HTML template

<div id="posts">
        {% for posts in posts %}
            <ul>
                <li>
                    <h4>{{ posts.user }} || {{ posts.timestamp }}</h4>
                    <h3>{{ posts.post }}</h3>
                </li>
            </ul>   
        {% empty %}
            <h6>No post availabel            
  • Related