python - django-taggit 公共(public)标签仅从基本 url 可见

标签 python django django-models django-templates django-taggit

我有一个基本的应用程序,用户可以在其中创建组。这些组可以有多个标签(通过 django-taggit),一旦发布,就会呈现在主页上,从新到旧排序。

在我的 views.py 中,我有主页使用的两个不同的标签列表:所有标签的列表和常用标签的列表:

class Home(ListView):
   model = Group
   template_name = 'home.html'
   ordering = ['-posted']

   def get_common_tags(self):
          # common tags
       common_tags = Group.tags.most_common()[:2]
       return common_tags

   def get_context_data(self, **kwargs):
       kwargs['common_tags'] = self.get_common_tags()
       return super().get_context_data(**kwargs)

class TagFilter(ListView):
   model = Group
   template_name = 'home.html'
   context_object_name = 'group'
   
   def get_queryset(self):
          # all tags
       tag_list = Group.objects.filter(tags__slug=self.kwargs.get('tag_slug'))
       return tag_list

   def get_context_data(self, *args, **kwargs):
       tag_list = Group.objects.filter(tags__slug=self.kwargs.get('tag_slug'))
       tag_list_is_empty = True
       if tag_list.exists():
           tag_list_is_empty = False
       context = super(TagFilter, self).get_context_data()
       context["tag_list_is_empty"] = tag_list_is_empty
       return context

这个想法是,当您点击一个标签时,它会根据该标签过滤分组结果:

{% get_taglist as tags %}


    <div>
      Filter by tag:
      {% for tag in tags %}
        <a href="{% url 'search_by_tag' tag.slug %}">#{{tag.name}}</a>
      {% endfor %}
    </div>

    <div>
      Common tags: 
      {% for tag in common_tags %}
        <a href="{% url 'search_by_tag' tag.slug %}">#{{tag.name}}</a>
      {% endfor %}
    </div>

    {% if tag_list_is_empty == True %}
      <p>Sorry! No group found with this tag</p>
    {% endif %}

    {% for group in object_list %}
      <a href="{% url 'group_detail' group.pk %}">
          <h1>{{group.name}}</h1>
          <p>{{ group.description|slice:":50"}}...</p>
      </a>
    {% endfor %}

我遇到的问题是我的常用标签列表仅在基本 url localhost:8000/ 处可见.但是当你点击一个标签时,它会将你带到一个 url localhost:8000/tags/slug , 常用标签列表消失。

我的第一个想法是,这与 common_tags 有关。在主页 View 中明确返回,一旦 url 更改,它就会停止显示。

这导致我将这两个函数移到 TagFilter 中,但后来我无法访问 tag_list_is_empty在我的模板中。我假设是因为你不能有两个 get_context_data功能相同。

我只是想知道保留所有这些变量并将它们传递给模板的最佳方法是什么,这样无论 url 是什么,标签列表都会显示,并且有条件地查看是否 tag_list_is_empty这样我就可以显示消息 <p>Sorry! No group found with this tag</p>

最佳答案

get_common_tags 中,您不需要请求 header 或任何东西。所以你可以从 Home 类中提取这个函数,你可以从多个 View 中使用/调用。

def get_common_tags(self):
    #consider using try/catch        
    try:
        return Group.tags.most_common()[:2]
    except:
        return {'no tag'}

class Home(ListView):
   model = Group
   template_name = 'home.html'
   ordering = ['-posted']

   def get_context_data(self, **kwargs):
       kwargs['common_tags'] = get_common_tags()
       return super().get_context_data(**kwargs)

class TagFilter(ListView):
   model = Group
   template_name = 'home.html'
   context_object_name = 'group'
   
   def get_queryset(self):
          # all tags
       tag_list = Group.objects.filter(tags__slug=self.kwargs.get('tag_slug'))
       return tag_list

   def get_context_data(self, *args, **kwargs):
       tag_list = Group.objects.filter(tags__slug=self.kwargs.get('tag_slug'))
       tag_list_is_empty = True
       if tag_list.exists():
           tag_list_is_empty = False
       context = super(TagFilter, self).get_context_data()
       context["tag_list_is_empty"] = tag_list_is_empty
       # add common tags to context
       context["common_tags"] = get_common_tags()
       return context

关于python - django-taggit 公共(public)标签仅从基本 url 可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72996190/

相关文章:

python - 重用封装在类中的变量和模型

python - 如何修复 django 1.5 中的 localflavor 弃用警告?

python - Django Pyinstaller .EXE 给我 ModuleNotFoundError : No module named 'app.urls'

python - 预取 Django 模型属性

python - 在Tkinter中设置错误处理循环

错误处理后 while 循环中的 Python 回溯错误

python - 配置 Vim 工作区以使用多种语言进行编程?

django - session 被 django-all auth 重置

python - 如何在两个 M2M 值上使用 prefetch_lated?

python - Django使用 'through'关系表从模型中获取所有相关对象