A
A
alxxla2020-04-17 07:04:44
Django
alxxla, 2020-04-17 07:04:44

Show articles related to a specific category?

views.py

class LanguageDetailView(View):
    def get_queryset(self):
        return Post.objects.filter(to_display=True)

    def get(self, request, language_slug=None, slug=None):
        if language_slug is not None:
            post = self.get_queryset().filter(language__slug=language_slug)
        elif slug is not None:
            post = self.get_queryset().filter(tags__slug=slug)
        else:
            post = self.get_queryset()
        return render(request, 'blog/language_detail.html', {'post_list': post})


urls.py
urlpatterns = [
    path('<slug:language_slug>/', views.LanguageDetailView.as_view(), name='post_list'),
    # path('<slug:slug>/', views.TagDetailView.as_view(), name='post_list_tag'),
    path('', views.MainView.as_view())
]


language_detail.html
{% for post in post_list %}
        <a href="{{ post.slug }}">{{ post.title|title }} </a>
        <p>{{ post.description }}</p>
    {% endfor %}


It seems to have done everything, the posts are not displayed (everything is fine with the template).
What needs to be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-04-17
@alxxla

Better learn how to debug than repeat questions. At least put print(post) before return render to understand what's wrong with post, or what error you make before it, django will display a bunch of detailed information for you.
ZY and what for you use View when there is ListView?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question