【发布时间】:2021-01-17 03:57:06
【问题描述】:
我正在尝试创建一个根据设备类别对设备进行分类的网站,当我在网页上链接 URL 时出现反向匹配错误。请我需要有关如何正确完成此操作的建议。提前致谢。
urls.py
from django.urls import path
from . import views
app_name = 'equipments_catalogue'
urlpatterns = [
path('equipment_list/', views.EquipmentListView.as_view(), name='equipment_list'),
path('equipment_categories/', views.EquipmentCategoryView.as_view(),
name='equipment_categories'),
path('equipment_by_category/<str:cats>/', views.EquipmentListByCategory,
name='equipment_by_category')
]
views.py
from django.shortcuts import render
from django.views.generic import ListView
from . import models
# Create your views here.
class EquipmentCategoryView(ListView):
model = models.Category
template_name = 'equipment_categories.html'
def EquipmentListByCategory(request, cats):
equipment_category = models.Equipment.objects.filter(category=cats)
return render(request, 'equipment_by_category.html', {'cats': cats , 'equipment_category':
equipment_category})
class EquipmentListView(ListView):
model = models.Equipment
template_name = 'equipment_list.html'
模板
{% extends 'base.html' %}
{% load static %}
{% block title %}Home{% endblock %}
{% block content %}
<h1>Welcome to JPI Equipment Categories Page</h1>
{% for category in object_list %}
<a href="{% url 'equipments_catalogue:equipment_categories' equipment_by_category.category
%}">{{ category.name }}</a><br><br>
{% endfor %}
{% endblock %}
【问题讨论】: