Django not serving text data points for template (home page)

As Ken points out:

So I can’t use a TemplateView.

The DetailView CBV requires a string or int like as a slug or pk in the url pattern. But for my use-case, I am just trying to create a home (landing) page for my site. The location in my web address bar needs to be: https://localhost:8000/. There must not be a slug or any string appended to the end or after the forward slash because the home page should be the ‘top level’ parent location of the whole site.

Here is the relevant section to my urls.py:

urlpatterns = [
    path('', HomeView.as_view(),name='home'),
    path('<str:slug>', AboutView.as_view(),name='about'),
    ...
]

As you can see above in my use case, the first parameter in the urlpatterns path function is an empty string. There is no place for a slug. How do I satisfy Django’s requirement for a DetailView to pass a slug / pk variable into the url pattern when the top level home (langing) page shouldn’t have one?

My DetailView CBV to handle the “about” page on the site (which outlines my organization’s mission / purpose) is similar but slightly different. The address location needs to be: https://localhost:8000/about. Since a slug is required for the DetailView, and since the address location is a string, I can use: ’<str:slug>’ in the url pattern this time. You can see that in the second path() function call above. But Django still throws a 404 when I navigate to https://localhost:8000/about:

image

I have been mindful by carefully adding about into the slug CharField inside the entry in the Admin Dashboard for this app. But I still get a 404. I continue to mishandle this slug properly.

TemplateView is the wrong way to go. I need a DetailView which requires a proper usage of the slug / pk variable. But I am doing something wrong.

In fact I already previously tried using a DetailView with some confusion: Resolving `AttributeError` for home / landing page routing with CBVs - #5 by Drone4four
The takeaway from that thread was that it doesn’t make sense to create a link on a page referring to itself using a slug and that it doesn’t make sense to use a slug for a home page. So I resolved to using a TemplateView which I am walking back by working with DetailViews again. Now I am back to my original problem with the way I am mishandling slugs.

As per ccbv.co.uk, and as you can see in the code snippets below, I leveraged this section of the doc: DeleteView -- Classy CBV

Someone else recommended the Django Class Based Views Diagrams website. Here is the specific location on that site for the DetailView Diagram.

The issues I have with this site are twofold:

  1. The diagrams show all the variables and attributes and functionality which is great, but there is no free-flowing prose connecting the dots. I guess that is what the official Django Docs are for and the two sites should be used together.
  2. The diagrams show source code as black font on a white background. Without syntax highlighting, it makes it very hard to follow along. It makes me queasy.

Here are the relevant snippets from my codebase that I am currently working with:

views.py:

from django.shortcuts import render, reverse
from django.views.generic import DetailView # TemplateView
from .models import Home, Podcast, About, Contact

class HomeView(DetailView):
   model = Home
   # fields = ['title','body']
   context_object_name = 'contents'   
   template_name = "landings/home.html"
   # def get_slug_field(self):
   #     self.objects.title

class AboutView(DetailView):
   model = About
   context_object_name = 'contents' 
   template_name = "landings/about.html"


   def get_slug_field(self):
       """
       Get the name of a slug field to be used to look up by slug as per: https://ccbv.co.uk/projects/Django/4.1/django.views.generic.edit/DeleteView/#get_slug_field      
       """
       return self.slug 

models.py:

from django.shortcuts import reverse
from django.db import models
from ckeditor.fields import RichTextField
from django.template.defaultfilters import slugify

class Home(models.Model):
   title = models.CharField(max_length=200)
   slug = models.SlugField(null=False, unique=True, blank=True)
   body = RichTextField(config_name='default',max_length=300000,blank=True, null=True)

   def __str__(self):
       return f'{self.title}'
 
class About(models.Model):
   title = models.CharField(max_length=200)
   slug = models.SlugField(null=False, unique=True, blank=True)
   body = RichTextField(config_name='default',max_length=300000,blank=True, null=True)