Skip to main content
 首页 » 编程设计

python之为什么一旦我使用 Tag.models.all() Taggit (Django-Tag) 就不能工作

2024年09月03日30dyllove98

在此示例中,我尝试使用 mixin 在“PropertyListing”的 ListView 上呈现标签之前,我已经成功实现了 django-taggit:

控制台不断告诉我:

NameError: name 'Tag' is not defined 

**问题显然来自 views.py 第 4 行。

我无法从像“PropertyListing”这样的模型中导入“Tag”,因为它是第三方库。

我试过导入

from taggit.managers import TaggableManager 

在 views.py 但同样的错误。

我正在使用 django 2.1 和 django-taggit 1.1.0

下面是代码:

模型.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}) 
 

View .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')) 
 

网址.py

path('', PropertyListingView.as_view(), name='property_listing'), 
    path('tag/<slug:slug>/', CityTaggedView.as_view(), name='city_tagged') 

任何帮助将不胜感激。我不明白为什么会这样。

请您参考如下方法:

您只需导入标签模型,例如:

# app/views.py 
 
# import the <b>Tag</b> class 
from taggit.models import <b>Tag</b> 
 
class TagMixin(object): 
    def get_context_data(self, **kwargs): 
        context = super(TagMixin, self).get_context_data(**kwargs) 
        context['tags'] = <b>Tag</b>.objects.all() 
        return context 
 
# ...

毕竟,您的代码使用了 Tag,但您从未在 views.py 文件中定义它。这与 Django 或 taggit 本身没有太大关系,只是 Python 不理解你对 Tag 的意思。