Home > Back-end >  My article doesn't display in article_details
My article doesn't display in article_details

Time:06-11

I want to show my articles but My article doesn't display on article_details.html

This is what my site looks like. You can see only my article's title works.

Articles had to be published there.

My models.py:

class Article(models.Model):
    title = models.CharField(max_length=50, verbose_name="Title")
    mini_description = models.TextField(max_length=100, default='', verbose_name='Mini_description')
    content = models.TextField(blank=True, verbose_name="Content")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="Date of add")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="Update date")
    photo = models.ImageField(upload_to='photos/', verbose_name="Photo")
    is_published = models.BooleanField(default=True, verbose_name="Is published")
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name="Category")

    def get_absolute_url(self):
        return reverse("article_details", kwargs={'pk': self.pk})

    def __str__(self):
        return self.title

    def __repr__(self):
        return f"Article(pk={self.pk}, title='{self.title}')"

    class Meta:
        verbose_name = "Article"
        verbose_name_plural = "Articles"
        ordering = ['-created_at']

My views.py:

class ArticleListByCategory(ListView):
    model = Article
    context_object_name = 'articles'
    template_name = 'blog/details.html'

    def get_queryset(self):
        sort_filed = self.request.GET.get('sorter')
        articles = Article.objects.filter(category_id=self.kwargs['pk'])
        if sort_filed:
            articles = articles.order_by(sort_filed)

        return articles

class ArticleDetails(ArticleListByCategory):
    model = Article
    context_object_name = 'article'
    template_name = 'blog/article_details.html'

    def get_queryset(self):
        article = Article.objects.filter(pk=self.kwargs['pk'], is_published=True).select_related('category')
        return article

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        article = Article.objects.get(pk=self.kwargs['pk'])
        context['title'] = f"Article: {article.title}"
        return context

My article_details.html:

{% extends "base.html" %}
{% load static %}


{% block style %}
<link href="{% static 'blog/css/main.css' %}" rel="stylesheet">
{% endblock style %}

{% block title %}
{{ title }}
{% endblock %}


{% block main %}

<section  id="articles">
    <div >
        <h1>{{ article.title }}</h1>
        <p><em>{{ article.mini_description }}
        </em>
        </p>
        <img src="">
        <div >
            <p>
                {{ article.content }}
            </p>
        </div>
        <div >
            {{ article.created_at| timesince }} ago
        </div>
    </div>
</section>

{% endblock main %}

I don't know why my article doesn't display. Please help me.

Why even StackOverflow doesn't want to publish my question It thought my question looks like mostly code. Ok. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

CodePudding user response:

What I'd suggest here is using the Django Generic View called DetailView. For example, you could do the following:

from django.views.generic import DetailView

class ArticleDetails(DetailView):
     context_object_name = 'article'
     template_name = 'blog/article_details.html'

     # Using the get_object method here, not get_queryset.
     def get_object(self, queryset=None):
         article = Article.objects.filter(pk=self.kwargs['pk'], is_published=True).select_related('category')
         return article

Ideally, that should work.

  • Related