简体   繁体   English

显示每个给定对象的所有Django-taggit标记

[英]Displaying all Django-taggit tags for each given object

I'm trying to display all the django-taggit tags related to the given object when I'm querying. 我正在尝试在查询时显示与给定对象相关的所有django-taggit标记。

I've tried adding this function within my search_result.html template like this but no tags are being displayed: 我尝试在我的search_result.html模板中添加此功能,但是没有显示任何标签:

{% if  page_obj.object_list %}
       <ol class="row top20">

{% for result in page_obj.object_list %}

          <div class="showcase col-sm-6 col-md-4">
           <a href="{{ result.object.get_absolute_url }}">
              <h3>{{result.object.title}}</h3>
              <img src="{{ result.object.image }}" class="img-responsive">
           </a>

<!-- I want to display them in the span below -->
      <div class="text-center">
          <span class="label label-info">{{ result.object.ptags.name }}</span>
      </div>


         </div>
{% endfor %}
      </ol>
   </div>
{% endif %}

My Models: 我的模特:

class Product(models.Model):
    title = models.CharField(max_length=255, default='')
    slug = models.SlugField(null=True, blank=True, unique=True, max_length=255, default='')
    description = models.TextField(default='')

    ptags = TaggableManager()

    timestamp = models.DateTimeField(auto_now=True)

    def _ptags(self):
        return [t.name for t in self.ptags.all()]

    def get_absolute_url(self):
        return reverse('product',
                       kwargs={'slug': self.slug})


    def __str__(self):
        return self.title

My custom forms.py function: 我的自定义forms.py函数:

from haystack.forms import FacetedSearchForm


class FacetedProductSearchForm(FacetedSearchForm):

    def __init__(self, *args, **kwargs):
        data = dict(kwargs.get("data", []))
        self.ptag = data.get('ptags', [])
        super(FacetedProductSearchForm, self).__init__(*args, **kwargs)

    def search(self):
        sqs = super(FacetedProductSearchForm, self).search()

        if self.ptag:
            query = None
            for ptags in self.ptag:
                if query:
                    query += u' OR '
                else:
                    query = u''
                query += u'"%s"' % sqs.query.clean(ptags)
            sqs = sqs.narrow(u'ptags_exact:%s' % query)

        return sqs

And I'm passing the forms into the views like this: 我将表单传递给这样的视图:

class FacetedSearchView(BaseFacetedSearchView):

    form_class = FacetedProductSearchForm
    facet_fields = ['ptags']
    template_name = 'search_result.html'
    paginate_by = 6
    context_object_name = 'object_list'

How can I do this? 我怎样才能做到这一点?

Can you try this instead 你可以尝试这个吗?

<span class="label label-info">{{ result.object.ptags.names }}</span>

You can loop over the ptags.names queryset to display individual tags, like this: 您可以遍历ptags.names以显示单个标记,如下所示:

{% for tag in result.object.ptags.names %}
    {{ tag }}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM