页面视图指的是 id,而路径不要求一个

Page view refers to id, whil path is not asking for one

我想加载一个默认的 Django 页面。没有什么花哨。但是,我收到的错误提示 id 设置不正确。

"Field 'id' expected a number but got 'zoekboek'."

这里令人困惑的地方(我是一个 django 初学者,所以如果这对你来说一点也不令人困惑,我不会感到惊讶):

代码

urls.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'),

]

views.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是使用正则表达式解析的; docs 在第 3 点中说:

  1. Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.

由于您的 URL 路径 path('<school_id>', views.school_detail, name='school_detail'), 非常通用,它匹配任何字符串,包括字符串 zoekboek;所以对 zoekboek 的请求落入你的 URL conf 的第二行,并被路由到视图 school_detail() 并且该视图需要 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', ...),

]