django - 页面视图是指 id,而路径不要求一个
问题描述
我想加载一个默认的 django 页面。没有什么花哨。但是,我得到的错误提示 id 设置不正确。
“字段 'id' 需要一个数字,但得到了 'zoekboek'。”
这里令人困惑的事情(我是一个 django 初学者,所以如果这对你来说一点也不令人困惑,我不会感到惊讶):
- urls.py 中此页面的路径不要求提供 id。
- 该视图尚未查询任何内容(我发现一些具有类似错误但与过滤器相关的帖子)。
- 调试信息指向另一个确实在请求 id 的视图。
- 当我在路径的开头添加斜杠时,错误消失了!
编码
网址.py
urlpatterns = [
path('', views.scholen, name='scholen'),
path('<school_id>', views.school_detail, name='school_detail'),
path('<school_id>/<groep_id>', views.school_groep, name='school_groep'),
path('<school_id>/<groep_id>/<UserProfile_id>', views.leerling_page, name='leerling_page'),
path('zoekboek', views.zoekboek, name='zoekboek'),
]
视图.py
from django.shortcuts import render, redirect, reverse, get_object_or_404
from books.models import Book, Rating
from .models import School, Groep
from profiles.models import UserProfile, Hobby, Sport
from django.contrib.auth.models import User
# Create your views here.
def scholen(request):
"""
Homepage for participating
schools.
"""
scholen = School.objects.all()
context = {
'scholen': scholen,
}
return render(request, 'schools/school_landing.html', context)
def school_detail(request, school_id):
"""
Details of individual schools.
"""
school = get_object_or_404(School, pk=school_id)
groep = Groep.objects.filter(school=school)
context = {
'school': school,
'groep': groep,
}
return render(request, 'schools/school_detail.html', context)
def school_groep(request, school_id, groep_id):
"""
Details of groep.
"""
school = get_object_or_404(School, pk=school_id)
groep = get_object_or_404(Groep, pk=groep_id)
a = groep.naam
kinderen = UserProfile.objects.filter(groep=a)
context = {
'school': school,
'groep': groep,
'kinderen': kinderen,
}
return render(request, 'schools/school_groep.html', context)
def leerling_page(request, school_id, groep_id, UserProfile_id):
"""
Personal page of school kids.
"""
profile = get_object_or_404(UserProfile, pk=UserProfile_id)
# If viewer is owner of page, viewer can edit
owner = False
if request.user == profile.user:
owner = True
context = {
'profile': profile,
'owner': owner,
}
return render(request, 'schools/leerling_page.html', context)
def zoekboek(request):
"""
Page for kids to search their favorite book
"""
context = {
}
return render(request, 'schools/zoek_boek.html', context)
这是足够的信息吗?
解决方案
简单的修复:path('zoekboek', views.zoekboek, name='zoekboek'),
从你的网址中的最后一个位置移动到第二个位置。
为什么?
因为 Django URL 是使用正则表达式解析的;文档在第 3 点中说:
- Django 按顺序遍历每个 URL 模式,并在与请求的 URL 匹配的第一个模式处停止,与
path_info
.
由于您的 URL 路径path('<school_id>', views.school_detail, name='school_detail'),
非常通用,因此它匹配任何字符串,包括字符串zoekboek
;因此请求zoekboek
落入您的 URL conf 中的第二行并被路由到视图school_detail()
,并且该视图需要 a school_id
。
建议:为了使 URL 处理更容易,因此您可以根据需要对 URL 路径进行排序,您可以稍微更改 URL 并添加前缀(例如school/
),以便没有任何字符串与 URL 路径匹配。例如,这应该有效:
urlpatterns = [
path('', ...),
path('school/<school_id>', ...),
path('school/<school_id>/<groep_id>', ...),
path('school/<school_id>/<groep_id>/<UserProfile_id>', ...),
path('zoekboek', ...),
]
推荐阅读
- c# - 如何使用实体框架查询具有动态列的表?
- javascript - ClientID 和 ClientSecret 如何在具有 OAuth2 身份验证的 NodeJS API 中工作?
- junit - 骆驼无法在模拟队列端点接收消息
- c++ - 使用 torch 编译的 C++ 程序,但无法在运行时加载 libtorch.so
- android - 单击 Activity 的其他 Fragments 时,有没有办法关闭 Window Popup。?
- php - 是否可以循环 $request->hasFile() 函数而不获取重复数据?
- grails - 如何将 logback 配置为从 Grails 3.x 上的 Bootstrap 以 INFO 级别记录?
- javascript - 动态单选按钮不会调用第二个 JavaScript 函数
- c - CS50 PSET3 恢复分段错误,无法获取输出 JPEG 的代码
- typescript - 基于对象定义 TypeScript 类型?