繁体   English   中英

Django-taggit,标签段未链接到正确的ListView

[英]Django-taggit, tag slug is not linking to the correct ListView

我安装了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上显示正确,但是当我单击其中的一个时,它链接到博客Post ListView模板并为空白,并且具有正确的子句,这是代码。

'''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 %}

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

最终,我发现了问题,两个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 %}

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

暂无
暂无

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

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