<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>
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",)
]
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