Reputation: 362
I have implemented django-taggit successfully until I was trying to use the mixin to render the tags on a ListView of "PropertyListing" in this example:
The console keep telling me:
NameError: name 'Tag' is not defined
**The PROBLEM comes from the views.py line 4 apparently.
I cannot import "Tag" from the model like "PropertyListing" since its a third party library.
I have tried to import
from taggit.managers import TaggableManager
in views.py but same error.
I am using django 2.1 and django-taggit 1.1.0
below is the code:
models.py
from taggit.managers import TaggableManager
class City(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=250, unique=True)
tags = TaggableManager()
class Meta:
verbose_name_plural = 'Cities'
def __str__(self):
return self.name
class PropertyListing(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=250, unique=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
description = models.TextField(max_length=1000)
address = models.CharField(max_length=1000)
is_active = models.BooleanField(default=False)
city = models.ForeignKey(City, on_delete=models.CASCADE, related_name='property_listings')
class Meta:
verbose_name_plural = 'Properties Listings'
def __str__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(PropertyListing, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('core:property_detail', kwargs={'pk': self.pk})
views.py
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 PropertyListingView(TagMixin, ListView):
model = City
model_type = PropertyImage
queryset = PropertyListing.objects.all()
context_object_name = 'properties'
template_name = 'core/property-listing.html'
def get_context_data(self, *args, **kwargs):
context = super(PropertyListingView, self).get_context_data(**kwargs)
context['cities'] = City.objects.all()
context['properties'] = PropertyImage.objects.select_related('image_property_listing')
return context
class CityTaggedView(TagMixin, ListView):
model = City
context_object_name = 'cities'
template_name = 'core/city-tagged.html'
def get_queryset(self):
return City.objects.filter(tags__slug=self.kwargs.get('slug'))
urls.py
path('', PropertyListingView.as_view(), name='property_listing'),
path('tag/<slug:slug>/', CityTaggedView.as_view(), name='city_tagged')
Any help would be much appreciated. I don't understand why this is happening.
Upvotes: 2
Views: 431
Reputation: 476493
You simply need to import the Tag
model, like:
# app/views.py
# import the Tag class
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
# ...
After all, your code makes use of a Tag
, but you never defined that in your views.py
file. This has not much to do with Django, or taggit itself, it is just Python thaat does not understand what you mean with Tag
.
Upvotes: 2