Hello all! I'm new in Django and have some problems understanding how I 
made this DetaiView work. 

I started changing my FBV to GCBV. This one, that the only thing it does is 
group articles by a tag:

def tag(request, tag_name):
> tag = get_object_or_404(Tag, tag_name=tag_name)
> articles = tag.article_set.all().order_by('-created_at')
> context = {
> 'tag': tag,
> 'articles': articles,
> }
> return render(request, 'default/index.html', context)
>

 I somehow made it work like this:

class TagDetailView(DetailView):
> model = Tag
> template_name = 'default/index.html'
> slug_field = 'tag_name' # get the tag name for the url
>
> def get_context_data(self, **kwargs):
> tag_ = self.kwargs.get('slug') # get the tag_name (slug)
> context = super().get_context_data(**kwargs)
> context['tag'] = get_object_or_404(Tag, tag_name=tag_)
> context['articles'] = context['tag'] \
> .article_set.all().order_by('-created_at')
> return context
>

This made sense to me, but I continued playing with it and still worked 
after I removed tag_ and context['tag']

class TagDetailView(DetailView):
> model = Tag
> template_name = 'default/index.html'
> slug_field = 'tag_name' # get the tag name for the url
>
> def get_context_data(self, **kwargs):
> context = super().get_context_data(**kwargs)
> context['articles'] = context['tag'] \
> .article_set.all().order_by('-created_at')
> return context
>

How can both, {{ tag }} and {{ articles }} be rendering in the template? I 
really want to understand that before I continue. I have read the 
documentation, some articles and don get it. Thank you. 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/56c418c1-bd80-4bd7-b20c-9de257a9f1c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to