I’m trying to wire up a CBV with a Model and routing for a brand new site I am building.
When I try landing on the home page, Django is showing an “AttributeError” at /home: “type object ‘Home’ has no attribute ‘Home’”.
Here is my landings/views.py:
from django.shortcuts import render
from django.views.generic import DetailView
from .models import Home
class HomeDetailView(DetailView):
models = Home
queryset = Home.objects.order_by('title')
context_object_name = "content"
def get_slug_field(self):
self.models.Home.title
I realize the problem is with the way I am calling the get_slug_field. I tried this instead:
def get_slug_field(self):
self.Home.title
Then Django shows: AttributeError at /home: ‘HomeDetailView’ object has no attribute ‘Home’
I’ve identified that the above two lines are the problem. But I am just not sure of the right way to call the slug title attribute. Can anyone clarify how to properly call get_slug_field
?
Here is my web app’s landings/urls.py:
from django.contrib import admin
from django.urls import path, include
from .views import HomeDetailView
urlpatterns = [
path('<str:slug>', HomeDetailView.as_view(),name='home'),
]
landings/models.py:
from django.db import models
class Home(models.Model):
title = models.CharField(max_length=200)
body = models.TextField(null=True, blank=True)
And I’ve populated some placeholder data in the Admin Dashboard for this web app.
Prohect base urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('landings.urls')),