首页 > 解决方案 > 页面视图是指 id,而路径不要求一个

问题描述

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

“字段 'id' 需要一个数字,但得到了 'zoekboek'。”

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

编码

网址.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)

这是足够的信息吗?

标签: djangodjango-urls

解决方案


简单的修复:path('zoekboek', views.zoekboek, name='zoekboek'),从你的网址中的最后一个位置移动到第二个位置。

为什么?

因为 Django URL 是使用正则表达式解析的;文档在第 3 点中说:

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

]

推荐阅读