Return data with CBV using parameters in the Django url

Asked

Viewed 12 times

0

Good evening. I am trying to return the data of a query, and even managed to make the query work. The problem is when to send the data to the template. Always appears the error that has to be a dictionary and not a Queryset/list.

The error when it is right is "context must be a Dict rather than Queryset." The error when category does not exist is "Category matching query does not exist."

What can I do to fix?

py views.

class PostagemPorCategoriaView(ListView):
    template_name = 'posts/postagem_lista_por_categoria.html'
    model = Postagem

    def get_context_data(self, *, object_list=None, **kwargs):
        if self.kwargs.get('url', None):
            categoria = Categoria.objects.get(url=self.kwargs.get('url'))
            object_list = get_list_or_404(Postagem.objects.filter(categoria=categoria.pk))
        return object_list

py.

urlpatterns = [
    path('categoria/<slug:url>/', PostagemPorCategoriaView.as_view(), name='postagem_por_categoria'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • It seems that it is only returning an item of a category by the id, but it wants to recover the context and passes to the page that query. you want only one item or on the page a possibility to indicate the item and show or this linked to the list and want it to be only one item at a time.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.