Django , models object not displayed in views

im newbie in django. I have some question related to models. So, i was trying to display the model name and date to the views by iterating to all of them. But somehow they dont show up in the views, i tried to search in google but none fixed my problem. Im sorry if i asked some ridiculous question, but here is my code.
And also i already checked my models and theyre valid


from django.db import models

from django.utils import timezone
from django.utils.text import slugify

class Post(models.Model):
    title = models.CharField(max_length=30)
    body = models.TextField()
    time_post = models.DateTimeField(auto_now_add=True)
    time_edit = models.DateTimeField(editable=False,blank = True)
    slug = models.SlugField(editable=False, blank=True)

    def save(self):
        self.slug = slugify(self.title)
        self.time_edit =
        super(Post, self).save()

    def __str__(self):
        return "{}. {}".format(, self.title)


from django.shortcuts import render
from  .models import Post

def blog(request):
    posts = Post.objects.all(),
    context = {
        'title':'Blog ',
        'nav': [
        ['/', 'Index']
    return render(request,'blog/blog.html',context)

My blog.html

 {% extends "base.html" %}

{% load static %}
{% block app_css %} <!-- Custom CSS per app !-->
    <link rel="stylesheet" types="text/css" href = "{% static "blog/css/styleblog.css" %}"> <!-- CSS OVERIDE !-->
{% endblock app_css %}

{% block header %}
  <h1 class="display-4">Welcome to {{title}} | ChrisDjango</h1>
  <p class="lead">This was made in Django by {{contributor}}</p>
{% endblock header %}

{% block content1 %}
    {% for post in Post %}
        <h2>{{post.title}}</h2>   #THE TITLE AND TIMEPOST DIDNT SHOW UP
    {% endfor %}
{% endblock content1 %}


   {% load static %}
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    {%include "snippets/styles.html"%} <!--Bootstrap!-->

    {% block app_css %}
    <link rel="stylesheet" types="text/css" href = "{% static "blog/css/styleblog.css" %}"> <!-- Custom CSS per app !-->
    {% endblock app_css %}

    <title>{{title}} | ChrisDjango</title>
    <img id="banner" style="border-bottom: 15px solid #343A40;" src="{% static img %}" alt="Blog Banner">

    {%include "snippets/navbar.html"%}
    <div class="jumbotron">
        <div class="container text-white text-center">
            {% block header %}

            {% endblock header %}
            <hr class="my-4">
    <div class="container-fluid">
        <div class="container bg-white text-dark shadow" style="margin-top:-150px" id="myBody">
            {% block content1 %}

            {% endblock content1 %}
        <div class="container bg-secondary text-white shadow">
             {% block content2 %}
             {% endblock content2 %}
 {%include "snippets/scripts.html"%}

Sorry if my code looks really weird
Thank you

Stack Overflow Asked by Mxzero mxzero on November 15, 2021

2 Answers

2 Answers

Let's start with this edit in urls:

posts = Post.objects.all(),

should be

posts = Post.objects.all()

Note the dropped comma.

I have made that mistake MANY times and it is really hard to spot sometimes. Here is what happens when you have a trailing comma (you get an iterable wrapper)

enter image description here

If you still have a problem LMK.

Answered by Marc on November 15, 2021

There is a trailing comma at the end of:

    posts = Post.objects.all(),
#              trailing comma ^

this means you wrap the item in a singleton tuple. It is thus a tuple with one element, the collection of object.

You should remove the comma at the end:

    posts = Post.objects.all()

I would furthermore rename 'Post' to posts, since this gives a hit that it is a collection of items.

Answered by Willem Van Onsem on November 15, 2021

Add your own answers!

Related Questions

Ask a Question

Get help from others!

© 2021 All rights reserved.