【问题标题】:Django-taggit, tag slug is not linking to the correct ListViewDjango-taggit,标签蛞蝓没有链接到正确的 ListView
【发布时间】:2019-08-29 00:52:25
【问题描述】:

我安装了 django-taggit 和 django-taggit-templatetags2 并包含在我需要的所有模型中。在带有模型 Post 的应用名称博客上,标签与下面的代码一样完美。

'''blog/views.py'''
def tutorialpage(request):
    tutorial_list = Post.objects.all()
    context = {
        'tutorial_list': queryset,
    }
    return render(request, "tutorialpage.html", context)

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 TagIndexView(TagMixin, ListView):
    template_name = 'tutorialpage.html'
    model = Post
    context_object_name = 'tutorial_list'

    def get_queryset(self):
        return Post.objects.filter(tags__slug=self.kwargs.get('slug'))

'''blog/urls.py'''
path('tag/<slug:slug>/', views.TagIndexView.as_view(), name='tagged'),

'''tutorialpage.html'''
{% load taggit_templatetags2_tags %}
{% for tutorial in tutorial_list %}
<a href="/tutorialdetail/{{tutorial.slug}}">{{ tutorial.title }}</a>
{% endfor %}
{% get_taglist as tags for 'blog.Post' %}
{% for tag in tags %}
<a href="{% url 'tagged' tag.slug %}">{{ tag.name }}</a>
{% endfor %}

现在使用不同的应用名称文章与模型 ArticlePost。我应用了相同的想法,并且 ArticlePost 中的标签显示正确,但是当我单击其中一个时,它链接到博客帖子 ListView 模板和空白,并且它具有正确的 slug,这是代码。

'''articles/view.py'''
def articlepost(request):
    article_list = ArticlePost.objects.all()
    context = {
        'article_list': article_list,
    }
    return render(request, "articlepost.html", context)

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 TagIndexView(TagMixin, ListView):
    template_name = 'articlepost.html'
    model = ArticlePost
    context_object_name = 'article_list'

    def get_queryset(self):
        return Post.objects.filter(tags__slug=self.kwargs.get('slug'))

'''articles/urls.py'''
path('tag/<slug:slug>/', views.TagIndexView.as_view(), name='tagged'),

'''articlepost.html'''
{% load taggit_templatetags2_tags %}
{% for article in article_list %}
<a href="/articledetail/{{article.slug}}">{{ article.title }}</a>
{% endfor %}
{% get_taglist as tags for 'articles.ArticlePost' %}   
{% for tag in tags %}
<a href="{% url 'tagged' tag.slug %}">{{ tag.name }}</a>
{% endfor %}

任何帮助将不胜感激,干杯

【问题讨论】:

  • 今天早上我在articles/view.py 上注意到TagIndexView 类,get_queryset 返回的是Post 而不是ArticlePost。我已经修复了,但错误仍然存​​在

标签: django django-taggit


【解决方案1】:

最后我发现了问题,两个 url 路径相同,django 在第一个匹配项中。所以我更改了其中一个的名称,也更改了类视图的名称以确保。

'''urls.py'''
path('taging/<slug:slug>/', views.TagPostView.as_view(), name='taggeded'),

'''article post.html'''
{% get_taglist as tags for 'articles.ArticlePost' %}

{% for taging in tags %}
       <a href="{% url 'taggeded' taging.slug %}">{{ taging.name }}</a>
{% endfor %}

名称没有任何意义,但没关系。现在它完美运行了。

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2018-04-22
    • 2011-02-09
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2018-08-27
    • 2010-09-30
    相关资源
    最近更新 更多