Django and it's selective functioning.

So Django seems to be having some sort of selective crisis because I just cannot get it to show exactly the same variables on one page, but have so issue with them showing on another.

I would have been fair enough it the same result happened across any template I applied this to, but it works on one template, but not another’s. I am so frustrated.

I just cannot get the Pills to show on the blog.html page

#blog.html
{% for object in objects %}
                <div class="blog-index-wrapper__flashcard">
                    <a href="/blog/{{object.slug}}" title="{{ object.BlogTitle }}">
                        <img src="{{ object.BlogImage.url }}" alt="">
                    </a>
                    <p class="blog-index-wrapper__blog-date">{{ object.BlogDate }}</p>
                    <a href="/blog/{{object.slug}}" title="{{ object.BlogTitle }}">
                        {{ object.BlogTitle }}
                    </a>
                    <div class="blog-index-wrapper__pill-container">
                        {% for Pill in object.get_blog_tags %}
                            <div>{{ Pill | title }}</div> 
                            {% empty %}
                                <p>No pills available</p:>
                        {% endfor %}
                    </div>
                </div>
            {% endfor %}

But have no issue with them showing up on this page

#blog-details.html
<div class="blog-article-container__block-element blog-article-container__pill-container blog-article-container__block-element--flex blog-article-container_block-element--text-wrap">
        {% for Pill in object.get_blog_tags %}
            <div>{{ Pill | title }}</div> 
        {% endfor %}
      </div>

This is my models.py

from django.db import models

# Create your models here.

class Blog(models.Model):

    BlogTitle       =       models.CharField(max_length=255, verbose_name="Blog Title")
    BlogSummary     =       models.TextField(default="Hello", null=True, verbose_name="Blog Summary")
    BlogContent     =       models.TextField(default="Content", null=True, verbose_name="Blog Content")
    BlogImage       =       models.ImageField(default="",upload_to='assets/', verbose_name="Blog Image")
    BlogDate        =       models.DateField(auto_now=True, verbose_name="Date of Publication")
    slug            =       models.SlugField(max_length=225,default="", null=True, unique=True, verbose_name="Slug")
    BlogPills       =       models.CharField(max_length=100,default="Blog", verbose_name="Blog Category Pills")
     
    def get_blog_tags(self):
        print("BlogPills:", self.BlogPills)
        tags = self.BlogPills.split(',') if self.BlogPills else []
        print("Tags:", tags)
        return tags

    class Meta:
        verbose_name_plural = "Blog"

def __str__(self):
    return self.BlogTitle

My views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from . models import Blog
from blauwestadtech import utils

def blog(request):
    objects                 = Blog.objects.all().values()
    meta_object__title	    = utils.meta_object__title("Blog")
    meta_object__narrative 	= utils.meta_object__narrative("Take a look at our company Blog. Keep updated. Stay informed.")
    meta_object__image 	    = utils.meta_object__image()
    template  = loader.get_template('blog.html')

    context   =  {
        'objects'                   :   objects,
        'meta_object__title' 		:	meta_object__title,
        'meta_object__narrative' 	:	meta_object__narrative,
        'meta_object__image' 		:	meta_object__image
    }

    return HttpResponse(template.render(context, request))

def blog__details(request, slug):
    object                  = Blog.objects.get(slug=slug)
    meta_object__title	    = utils.meta_object__title(object.BlogTitle)
    meta_object__narrative 	= utils.meta_object__narrative(object.BlogSummary)
    meta_object__image 	    = utils.meta_object__image()
    template                = loader.get_template('blog-details.html')
    read_time               = utils.estimated_read_duration(object.BlogContent)

    context   = {
        'object'                    :   object,
        'meta_object__title' 		:	meta_object__title,
        'meta_object__narrative' 	:	meta_object__narrative,
        'meta_object__image' 		:	meta_object__image,
        'read_time '                :   read_time 
    }
    return HttpResponse(template.render(context, request))

I am literally pulling my hair out. I just cannot understand why Django refuses to display the relevant information on the blog.html page but not on the blog-details.html page.

There are no CSS clashes as I have removed all CSS attributes.

In the blog.html it’s returning the No pills available. Why it Django telling me there are no pills available when you go to the blog itself and the pills are display correctly? Why is Django telling me there are no pills available when there are pills in the model?

I have debugged and no pills are being printed, I have made migrations, I have debugged again and still no pills are being printed, I have made migration again and it’s still the same result, Django just does not want to know.

The two pages are being served exactly the same information. One age has decided the show the information, the other page just doesn’t want to know.

Bottom line first, the issue is here:

objects         = Blog.objects.all().values()
                                     ^^^^^^^^

In your blog__details view, you’re supplying objects to the template rendering engine:
object = Blog.objects.get(slug=slug)

This allows the template engine to call the model method get_blog_tags.

However, in the blog view, you’re using the values function to convert the queryset being returned to objects to a list of dicts. The dict entries that you are iterating over in your template do not have a get_blog_tags method.

Remove the call to values() from your query in the blog view.

Also, see the docs at QuerySet API reference | Django documentation | Django to see the lists of functions that return a queryset and those that don’t. Being aware of these differences is important.

Thank you, I changed it to this

objects                 = Blog.objects.all()

I still had the same resulted, however, I think It just needed a refresh anyway.