Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么Taggit(Django Tag)赢了';一旦我使用Tag.models.all()就无法工作_Python_Django_Python 3.x_Django Views_Django Taggit - Fatal编程技术网

Python 为什么Taggit(Django Tag)赢了';一旦我使用Tag.models.all()就无法工作

Python 为什么Taggit(Django Tag)赢了';一旦我使用Tag.models.all()就无法工作,python,django,python-3.x,django-views,django-taggit,Python,Django,Python 3.x,Django Views,Django Taggit,我已经成功地实现了django taggit,直到我尝试使用mixin在本例中的“PropertyList”的ListView上呈现标记: 控制台一直在告诉我: NameError: name 'Tag' is not defined **问题显然来自views.py第4行 我不能像“PropertyList”那样从模型中导入“Tag”,因为它是第三方库 我已经试着进口了 from taggit.managers import TaggableManager 在views.py中,但出现相同

我已经成功地实现了django taggit,直到我尝试使用mixin在本例中的“PropertyList”的ListView上呈现标记:

控制台一直在告诉我:

NameError: name 'Tag' is not defined
**问题显然来自views.py第4行

我不能像“PropertyList”那样从模型中导入“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'))

url.py

path('', PropertyListingView.as_view(), name='property_listing'),
    path('tag/<slug:slug>/', CityTaggedView.as_view(), name='city_tagged')
path(“”,PropertyListingView.as_view(),name='property_listing'),
路径('tag/',CityTaggedView.as_view(),name='city_taged')

任何帮助都将不胜感激。我不明白为什么会发生这种情况。

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

# 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

# ...
#app/views.py
#导入标记类
从taggit.models导入标记
类TagMixin(对象):
def获取上下文数据(自身,**kwargs):
context=super(TagMixin,self)。获取上下文数据(**kwargs)
上下文['tags']=Tag.objects.all()
返回上下文
#…
毕竟,您的代码使用了
标记
,但您从未在
views.py
文件中定义过它。这与Django或taggit本身没有多大关系,只是Python thaat不理解您使用
Tag
的意思