0

Hello everyone I have just started developing my blog with django but I don't get to display my posts individually. DetailView doesn't work, please help!

here is my url

from django.urls import path
from .views import Home, ArticleDetailView


urlpatterns = [
    path('', Home.as_view(), name='home'),
    path('article/<int:pk>', ArticleDetailView.as_view(), name='article_details'),
]

Here is my view.

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Article

# Create your views here.
class Home(ListView):
    model = Article
    template_name = 'home.html'

class ArticleDetailView(DetailView):
    model = Article
    template_name = 'article_detail_view.html'

My model

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Article(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField()

    def __str__(self):
        return self.title + '|' + str(self.author)

    class Meta:
        verbose_name = 'Article'
        verbose_name_plural = 'Articles'

Home.html

 <h2>Post</h2>
<ul>
    {% for post in object_list %}
        <li><a href="{% url 'article_details' post.pk %}">{{post.title}} by {{ post.author }}</a></br>
        <p>{{post.body}}</p></li>
    {% endfor %}
    
</ul>

article_detail_view.html

<h2>Article Detail View</h2>

<h2>{{post.title}} By {{ post.author }}</h2></br>
<p>{{post.body}}</p>

man980
  • 9
  • 2
  • How you are accessing this detail view pls mention your url conf and doest it have data? – Ashish Nautiyal Oct 04 '22 at 04:44
  • Can you add the code for your template, please. – Abdullah Oct 04 '22 at 05:12
  • Code of my templates added. – man980 Oct 04 '22 at 05:15
  • What does "DetailView doesn't work" mean in particular? What happens or doesn't happen? – AKX Oct 04 '22 at 05:22
  • 1
    **Typo**: Your model is named `Article` so by default you should be able to get the object as either `article` or as `object` but you are writing `{{post.title}}` (you should write `{{ article.title }}`, etc.) and so on... If you really want the object to be named "post" you have to specify [context_object_name](https://docs.djangoproject.com/en/4.1/topics/class-based-views/generic-display/#making-friendly-template-contexts) – Abdul Aziz Barkat Oct 04 '22 at 05:40

3 Answers3

0

Thanks for updating the question.

First add the get_absolute_url method to the model as follows:

class Article(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField()

    def __str__(self):
        return self.title + '|' + str(self.author)

    class Meta:
        verbose_name = 'Article'
        verbose_name_plural = 'Articles'
        
    def get_absolute_url(self):
        return reverse("article_details", kwargs={"pk": self.pk})

In your article_detail_view.html, you can access the article using the article instance:

<h2>Article Detail View</h2>

<h2>{{article.title}} By {{ article.author }}</h2></br>
<p>{{article.body}}</p>
Abdullah
  • 624
  • 6
  • 15
  • 1
    `get_absolute_url()` isn't necessary, since OP is using `{% url %}` in their template anyway. – AKX Oct 04 '22 at 05:23
0

in your article_detail_view.html you can access the article by: {{object.title}} you can also specify what to return in template and how you wanna name it by overriding get_context_data check this

Zourka
  • 1
  • 3
-1

in models.py

class Meta:
    managed = True
    verbose_name = 'Article'
    verbose_name_plural = 'Articles'
brenin
  • 3
  • 2