Seite wählen

Michael recommends the Classy Class-Based Views website to gain a basic understanding.
They created a great overview of all class-based views in Django (even 4.0) including brief summaries of their purpose and functionality to empower people choosing the most suitable one.
Starting at the main page I was quickly able to identify the DetailView as the obviously most suitable one to create a detail view of my Ad object:

Render a „detail“ view of an object.

By default this is a model instance looked up from self.queryset, but the view will support display of any object by overriding self.get_object().

BaseDetailView or DetailView: Choosing between two options🤔

But the BaseDetailView (which is an ancestor of DetailView in the Method Resolution Order (MRO)) could be suitable as well:

A base view for displaying a single object.

I am wondering: What’s the difference between these two views?

Following the Documentation button on the BaseDetailView page they bring me to the respective part in the Django documentation, which says:

class django.views.generic.detail.BaseDetailView

A base view for displaying a single object. It is not intended to be used directly, but rather as a parent class of the django.views.generic.detail.DetailView or other views representing details of a single object.

So this is not what I need, since it’s not intended to be used directly.
Hence it’s rather used to build the base for the actual DetailView (in my understanding).
Therefore: Back to the DetailView!

DetailView: Implementation in a real world Django project🐍

Investigating the documentation of the DetailView you find an applicable example for the complete implementation (inlcuding the view, the URLconf (urls.py) and the HTML template).

side note:

Django’s documentation has the Built-in class-based views API, which gives an overview of all views and mixins in Django. In the Base vs Generic views section they actually describe that Generic views are the easier and quicker approach, while Base views are just serving as the base for the other class based views (like the name suggests😉):

Django’s generic views are built off of those base views, and were developed as a shortcut for common usage patterns such as displaying the details of an object. They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself.

The above mentioned Built-in class-based views API has a link to the Class-based views topic guide at the top, which gives a neat and brief summary of the usage of class-based views.👍


Based on the example in the DetailView documentation, I’ll now try to implement the detail view for my Ad model using the class-based, generic display view DetailView.

Since I completely didn’t understand how to use the get_context_data() method in the examples for the DetailView in the Django docs, I’ve quickly searched on YouTube for detailview django.
I found the five minutes video from CodingEntrepreneurs and watched the first 45 seconds.
Voilà I know how to implement my DetailView without confusing methods, but only using the most simple basics:

views.py

from django.views.generic.detail import DetailView
from .models import Ad
...
class AdDetailView(DetailView):
    template_name = 'codermatch/detail.html'
    queryset = Ad.objects.all()

urls.py

from django.urls import path
from .views import AdDetailView

app_name = 'codermatch'
urlpatterns = [
    ...
    path('ad-detail/<int:pk>/', AdDetailView.as_view(), name='adDetail'),
]

detail.html

{% extends 'base.html' %}

{% block main %}
<h1>{{ object.adTitle }}</h1>
<p>{{ object.adDescription }}</p>
{% endblock main %}

side note

I am using the generic object.-notation in the detail.html.
Instead one could also use the specific model name.
The model I defined in models.py is class Ad(models.Model).
Therefore the detail.html could also look like this in my case:

{% extends 'base.html' %}

{% block main %}
<h1>{{ ad.adTitle }}</h1>
<p>{{ ad.adDescription }}</p>
{% endblock main %}

DetailView: Simplest approach to get started

After complaining and wondering a lot regarding the Django docs, I’ve figured out the easiest and most comprehensive way for myself.
Therfor I’ve combined the approaches from the DetailView docs and the above mentioned video as follows.

views.py

from django.views.generic.detail import DetailView
from .models import Ad
...
class AdDetailView(DetailView):
    model = Ad
    template_name = 'codermatch/detail.html'

The other files (urls.py and details.html) stay the same as in the approach above.
In this approach here I’ve used the model = Ad part from the exmaple in the Django docs of DetailView.
I know that I can use the template_name because it’s listed as Attribute in the CCBV documentation of DetailView.
And to bear in mind: It’s also used like this in the approach above.

get_context_data: How does it work? (remaining questions)❓

  • How does the get_context_data() method do? How does it work?
    • And why is the Django docs so complicated when describing how to implement views (using examples which are not applicable for beginners, since they are using get_context_data(), which you actually don’t need if you just want to setup a simple view😣)?
  • Is it possible to implement template_names similar to url tags (in Django template language), in a dynamic way rather then hardcoding them all?