tag urls please help!

11 views
Skip to first unread message

sofutou...@gmail.com

unread,
Apr 3, 2019, 1:21:43 PM4/3/19
to django-taggit
I have a problem. i cant sent a right url when clicked on a tag link. it always show 404 error pages. please help. sorry for my english.

my index.html
<section>
       
<ul>
           
<li>Tags:
                {% for menu in menu %}
                    {% for tag in menu.tags.all %}
                       
<a href="{{ siteUrl }}tags/{{ tag }}">{{ tag }}</a>
                    {% endfor %}
                {% endfor %}
           
</li>
       
</ul>

</section>

when i click on a tag, it will link like .../tags/<tag>. but it shown 404 page not found.

urls.py
from django.urls import path
from . import views
from .views import TagListView, FoodListView

urlpatterns
= [
    path
('', views.index),
    path
(r'^tags/(?P<tag>[-\w]+)/$', FoodListView.as_view(), name="menu_list"),
    path
(r'^tags/(?P<slug>[a-z0-9_-]+)', TagListView.as_view(), name="tag-list",)
]

and views.py
from django.http import  HttpResponse
from django.shortcuts import render
from django.views.generic import ListView
from .models import Menu
from taggit.models import Tag
from django.shortcuts import render, get_object_or_404


def index(request):
    menu
= Menu.objects.all()
   
return render(request, 'index.html', {'menu': menu})


class FoodListView(ListView):
    template_name
= "list.html"
    context_object_name = "food"


class TagListView(ListView):
    template_name
= "list.html"

    def get_queryset(self):
       
return Menu.objects.filter(tags__slug=self.kwargs.get("slug")).all()

   
def get_context_data(self, **kwargs):
        context
= super(TagListView, self).get_context_data(**kwargs)
        context
["tag"] = self.kwargs.get("slug")
       
return context


Reply all
Reply to author
Forward
0 new messages