I’m trying to display the name of the current tag, i.e. the one we are in. Does not work. How to write correctly to display a tag in a template?
<code> #Models
from django.db import models
from taggit.managers import TaggableManager
class Blog(models.Model):
title = models.CharField(max_length=150)
created_at = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=550)
tags = TaggableManager()
def __str__(self):
return self.title
#Views
from django.shortcuts import render
from django.views.generic import ListView
from .models import *
from taggit.models import Tag
class TagMixin(object):
def get_context_data(self, **kwargs):
context = super(TagMixin, self).get_context_data(**kwargs)
context['tags'] = Tag.objects.all()
return context
class PostIndexView(TagMixin,ListView):
model = Blog
template_name = 'blog.html'
queryset=Blog.objects.all()
context_object_name = 'posts'
class TagIndexView(TagMixin,ListView):
model = Blog
template_name = 'blog.html'
context_object_name = 'posts'
def get_queryset(self):
return Blog.objects.filter(tags__slug=self.kwargs.get('tag_slug'))
#Templates
{% if tag %}
<h3>Posts by tag: {{ tag }}</h3> <!-- Does not display tag name -->
{% endif %}
</code>
<code> #Models
from django.db import models
from taggit.managers import TaggableManager
class Blog(models.Model):
title = models.CharField(max_length=150)
created_at = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=550)
tags = TaggableManager()
def __str__(self):
return self.title
#Views
from django.shortcuts import render
from django.views.generic import ListView
from .models import *
from taggit.models import Tag
class TagMixin(object):
def get_context_data(self, **kwargs):
context = super(TagMixin, self).get_context_data(**kwargs)
context['tags'] = Tag.objects.all()
return context
class PostIndexView(TagMixin,ListView):
model = Blog
template_name = 'blog.html'
queryset=Blog.objects.all()
context_object_name = 'posts'
class TagIndexView(TagMixin,ListView):
model = Blog
template_name = 'blog.html'
context_object_name = 'posts'
def get_queryset(self):
return Blog.objects.filter(tags__slug=self.kwargs.get('tag_slug'))
#Templates
{% if tag %}
<h3>Posts by tag: {{ tag }}</h3> <!-- Does not display tag name -->
{% endif %}
</code>
#Models
from django.db import models
from taggit.managers import TaggableManager
class Blog(models.Model):
title = models.CharField(max_length=150)
created_at = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=550)
tags = TaggableManager()
def __str__(self):
return self.title
#Views
from django.shortcuts import render
from django.views.generic import ListView
from .models import *
from taggit.models import Tag
class TagMixin(object):
def get_context_data(self, **kwargs):
context = super(TagMixin, self).get_context_data(**kwargs)
context['tags'] = Tag.objects.all()
return context
class PostIndexView(TagMixin,ListView):
model = Blog
template_name = 'blog.html'
queryset=Blog.objects.all()
context_object_name = 'posts'
class TagIndexView(TagMixin,ListView):
model = Blog
template_name = 'blog.html'
context_object_name = 'posts'
def get_queryset(self):
return Blog.objects.filter(tags__slug=self.kwargs.get('tag_slug'))
#Templates
{% if tag %}
<h3>Posts by tag: {{ tag }}</h3> <!-- Does not display tag name -->
{% endif %}
P.s. The site asks for more description, but what else can be described here? The problem with the output is described
New contributor
Ajex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.