Side note #1: I won’t comment on any tutorials other than the Official Django Tutorial and the Django Girls Tutorial. There are too many out there and I can’t speak to what they address or ignore, or how they present their material - or more importantly, how they define the concepts being used.
It’s not the DetailView that is the issue. It’s your data models that is causing you these difficulties.
First, I think you should work on a change in your “mental model” of what’s going on here. You should stop thinking in terms of “pages”, and more in terms of “views rendering data as content within pages”. It’s a fairly crucial distinction when working in Django.
What does that mean here? Specifically, what you don’t have is a “home” page. Also, you don’t have an “about” page. You have one or more pages that are going to display content. (That “content” is to come from the data stored in your models.)
At one URL (e.g. /home/
), you’re going to display the content from an instance of a model.
At a different URL (e.g. /about/
), you’re going to display the content from an instance of a model.
They might be the same model, They might be different models. They might use the same template, the might use different template. Those distinctions don’t matter here yet.
Let’s look at the models you posted:
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)
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)
What you have defined here is a model named Home. That means that this is going to create a table in your database called something like myapp_home
. That table is capable of storing some number of instances of Home
.
But, you’re saying you only have one bit of content for Home - therefore, your data model may be incorrect for the project you’re trying to build.
Repeat this logic for About
. Same thoughts, same conclusion.
So the root issue at this point is that you haven’t appropriately defined your models to support the application you’re trying to build.
The next thing I see is that these two model definitions are identical. From what you’ve identified so far, I see no reason to make these two separate models.
So let’s define a new model:
class PageContent(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)
and to this, I’m going to add a new field:
page_type = models.CharField(max_length=20)
What this means is that now, I can store both my Home
and About
content in this model, perhaps using page_type
of home
and about
, respectively.
This allows me to retrieve the content for /home/
by PageContent.objects.get(page_type='home')
. And this provides you with the information you need to proceed with your DetailView to display this content.
Going this route does require some additional work along the way - you want to ensure that your initial creation of these pages set the proper page_type
You probably want to ensure that you never end up with multiple rows for either page_type home
or about
.
As a blog, you may even want to think about unifying this data with the rest of your blog’s data structures - that’s something only you can address.
But you do want to change your perspective on how you think about this.
- You’re not creating pages in your database.
- You’re adding data to your database that will be used by your views to create pages.
So with that in mind:
Conceptually, this is incorrect.
A ListView lists data. Some of that data are links to other URLs. Those URLs will (generally) read other data to produce pages.
Again, don’t think in terms of “pages”. Think in terms of URLs causing views to be called - which can read data to produce pages. (A view is not a “page”. A view is python code that produces something as a response. That response may/may not be HTML and may/may not be a complete page.)