【问题标题】:Why Taggit (Django-Tag) won't work once I used Tag.models.all()为什么一旦我使用 Tag.models.all() Taggit (Django-Tag) 就无法工作
【发布时间】:2019-11-11 03:14:02
【问题描述】:

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

控制台不断告诉我:

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

下面是代码:

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

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

【问题讨论】:

    标签: python django python-3.x django-views django-taggit


    【解决方案1】:

    您只需要导入Tag 模型,例如:

    # 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
    
    # ...

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

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      相关资源
      最近更新 更多