gpt4 book ai didi

python - 为什么我收到无反向匹配错误

转载 作者:行者123 更新时间:2023-12-04 08:08:39 24 4
gpt4 key购买 nike

您好,正在使用 Django 在一个新站点上工作。似乎无法弄清楚这个错误。首先,预期站点的结构如下:
主页 - 显示不同的语言,并带有指向您可以学习这些语言的国家/地区的链接。
单击主页上的国家/地区 -> 国家/地区页面
国家/地区页面将列出在该国家/地区教授该语言的所有学校。
单击国家页面上的学校 -> 学校页面。
我发现 Country 页面中的这行代码导致了这个问题,但我不知道如何解决它

<a href="{% url 'schools' schools.id %}">
<div class="left"><h4>Test Link</h4></div>
</a>
这是我在浏览器中得到的错误
NoReverseMatch at /5/ Reverse for 'schools' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<country_id>[0-9]+)/(?P<schools_id>[0-9]+)/$']
项目中的代码:
模型.py
from django.db import models

class Languages(models.Model):
language = models.CharField(max_length=100, blank=True, null=True)
country = models.ManyToManyField('Country', blank=True)
image = models.CharField(max_length=100, blank=True)

def __str__(self):
return self.language

class Country(models.Model):
country_name = models.CharField(max_length=50)
country_image = models.CharField(max_length=100, blank=True)
country_city = models.ManyToManyField('City', blank=True)

def __str__(self):
return self.country_name

class City(models.Model):
city_name = models.CharField(max_length=50)
city_image = models.CharField(max_length=1000, blank=True)
city_schools_in = models.ManyToManyField('Schools', blank=True)

def __str__(self):
return self.city_name

class Schools(models.Model):
school_name = models.CharField(max_length=100)
school_image = models.CharField(max_length=1000, blank=True)
school_country = models.ManyToManyField('Country', blank=True)
school_city = models.ManyToManyField('City', blank=True)
school_description = models.TextField()
school_accreditations = models.ManyToManyField('Accreditations', blank=True)
school_lpw = models.IntegerField()
school_cs = models.IntegerField()
school_course = models.ManyToManyField('CoursePrices', blank=True)

def __str__(self):
return self.school_name

class Accreditations(models.Model):
accreditation_name = models.CharField(max_length=50)

def __str__(self):
return self.accreditation_name

class CoursePrices(models.Model):
course_title = models.CharField(max_length=50, blank=True)
lessons_per_week = models.IntegerField()
class_size = models.IntegerField()
weekly_price = models.IntegerField()
language_level = models.CharField(max_length=50, blank=True)

def __str__(self):
return self.course_title
View .py
from django.shortcuts import render
from django.http import Http404

from .models import Schools, CoursePrices, Languages, Country, City


def home(request):
languages = Languages.objects.all()
return render(request, 'home.html', {
'languages': languages,
})

def country(request, country_id):
try:
country = Country.objects.get(id=country_id)
except Languages.DoesNotExist:
raise Http404('Language not found')
return render(request, 'country.html', {
'country': country,
})

def schools(request, country_id, schools_id):
try:
schools = Schools.objects.get(id=schools_id)
except Schools.DoesNotExist:
raise Http404('school not found')
return render(request, 'schools.html', {
'schools': schools,
})
网址.py
from django.contrib import admin
from django.urls import path

from schools import views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('<int:country_id>/', views.country, name='country'),
path('<int:country_id>/<int:schools_id>/', views.schools, name='schools'),
]
项目结构截图
Project structure screenshot
任何帮助将不胜感激。还请原谅发布或编码中的任何错误,对于编码和堆栈溢出/一般来说仍然很陌生:)

最佳答案

您的路径名为 schools需要两个参数,您只传递了一个,因此无法匹配任何路由。
您对 Rafael 的回答(即使提供了两个参数)仍然不匹配路径的评论是因为 urls.py 中路径的顺序文件。
Django 会按顺序遍历路径列表,因为路径只有一个参数 country_id首先找到它尝试但失败,因为您有第二个参数。
要解决这个问题,请更改路径的顺序

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
# next two lines have been swapped.
path('<int:country_id>/<int:schools_id>/', views.schools, name='schools'),
path('<int:country_id>/', views.country, name='country'),
]

关于python - 为什么我收到无反向匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66089849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com