【问题标题】:Reverse for 'equipment_categories' with arguments '('',)' not found. 1 pattern(s) tried: ['equipments/equipment_categories/$']未找到带有参数 '('',)' 的 'equipment_categories' 的反向操作。尝试了 1 种模式:['equipments/equipment_categories/$']
【发布时间】: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 %}

【问题讨论】:

    标签: python django sorting


    【解决方案1】:

    你说错了。正确的调用方式是 {% url ':' %}

    您的应用名称是equipments_catalogue,您的网址名称是equipment_by_category,而不是equipment_categories,这是正确的,并且您应该传递的参数应该是一个字符串,例如category.name

    {% url 'equipments_catalogue:equipment_by_category' category.name %}
    

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 2020-12-28
      • 1970-01-01
      • 2022-01-17
      • 2020-10-28
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      相关资源
      最近更新 更多