我正在尝试过滤教授表以显示与特定学校和特定专业相对应的教授,错误消息中显示了参数,我只是根本不理解为什么有错误消息
Views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import professor, School, Major, School_Major
def index(request):
schools = School.objects.all()
return render(request, 'locate/index.html', {'schools': schools})
# def Major(request, Major):
# major_choice = professor.objects.filter(Major =Major)
# return render(request, 'locate/major.html', {'major_choice': major_choice})
def Majors(request, school_pk):
schools_majors_ids = []
major_after_filter = []
#Filter to a show the association of 1 schools majors
school_choice = School_Major.objects.filter(school_id = school_pk)
#Append each of the major id's to school_majors_ids list
for store in school_choice:
schools_majors_ids.append(store.major_id)
#Filter majors names required
for store in schools_majors_ids:
name = Major.objects.get(id = store)
major_after_filter.append(name)
return render(request, 'locate/major.html', {'major_after_filter' : major_after_filter,
'school_pk' : school_pk})
def Professors(request, school_pk, major_pk):
professors = professors.objects.filter(school_id = school_pk).filter(major_id = major_pk)
return render(request, 'locate/professors', {'professors' : professors})
url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path(' <int:school_pk>/', views.Majors, name='Major'),
path(' <int:school_pk/<int:major_pk>/', views.Professors, name="Professors")
]
majors.html
<h3 id="test">{{school_pk}}</h3>
<ul>
{% for major in major_after_filter %}
<li><a href="{% url 'Professors' school_pk major.id %}">{{major.name}}</a></li>
{%endfor%}
</ul>
我需要一种方法来设置学校主键的值以及专业主键的值作为Profess视图功能的参数,以便能够筛选出Professor表以显示正确的专业。
当我离开school_pk时,如下所示
我收到以下错误
django.urls.exceptions.NoReverseMatch: Reverse for 'Professors' with arguments '(1, 5)' not found. 1 pattern(s) tried: ['locate/\\ <int:school_pk/(?P<major_pk>[0-9]+)/$']
当我删除如下所示的school_pk时
<li><a href="{% url 'Professors' major.id %}">{{major.name}}</a></li>
我收到此错误
TypeError: Professors() missing 1 required positional argument: 'school_pk'
这是有道理的,因为Professor函数需要一个school_pk参数(请注意,如果html和parameter中使用的变量令人困惑,则将其命名为相同的遗憾。
答案 0 :(得分:1)
您在网址格式中缺少右括号。
path('<int:school_pk>/<int:major_pk>/',
^