简体   繁体   中英

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

I installed django-taggit and django-taggit-templatetags2 and included in all the models that I needed. On the app name blog with model Post the tags work perfect as the code bellow.

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

Now on a different app name articles with model ArticlePost. I applied the same idea and the tags display correct from the ArticlePost, but when I click on one of them it links to the blog Post ListView template and blank, and it has the correct slug, here is the code.

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

Any help would be appreciated, Cheers

Finally I found the issue, both url path are the same and django falls in the first match. So I changed the name of one of them, also I changed the names of the class view just to make sure.

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

The names don't make any sense but it doesn't matter. Now it works perfect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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