TransWikia.com

Django , models object not displayed in views

Stack Overflow Asked by Mxzero mxzero on November 15, 2021

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

Models

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 = timezone.now()
        super(Post, self).save()

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

urls

from django.shortcuts import render
from  .models import Post

def blog(request):
    posts = Post.objects.all(),
    context = {
        'title':'Blog ',
        'contributor':'Chris',
        'img':'blog/img/BlogBanner.jpg',
        'Post':posts,
        'nav': [
        ['/blog/recent','Recent'],
        ['/blog/news','News'],
        ['/blog','Blog'],
        ['/about','About'],
        ['/', '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
        <p>{{post.time_post}}</p>
    {% endfor %}
{% endblock content1 %}

base.html

   {% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <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">

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

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

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

Sorry if my code looks really weird
Thank you

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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP