找不到关键字参数为“{school\u id”:“'}”的“ipads\u by\u school”的反向。尝试了1个模式:['(?P<school\u id>[^/]+)$']Djang

2024-05-16 05:53:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Django/Python比较陌生

在标题中生成NoReverseMatch错误的my index.html代码行如下所示

<a href="{% url 'ipads_by_school' school_id=LES %}" class="btn btn-outline-primary m-1" >LES</a>

这是我的url.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('<str:school_id>', views.ipads_by_school, name='ipads_by_school')
]

这是我的观点

from django.shortcuts import render
from django.http import HttpResponse

from .models import Ipad

def index(request):
    return render(request, 'ipads/index.html')

def ipads_by_school(request, school_id):
    school_ipads = Ipad.objects.filter(school_id__icontains=school_id)

    context = {
        'school_ipads': school_ipads
    }

    return render(request, 'ipads/ipads_by_school.html', context)

Tags: pathdjangofromimportidurlindexby