gpt4 book ai didi

python - 为什么当我交换 url 模式成员的顺序时,错误会有所不同

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

Note: I'm not requesting for a help to fix the errors, but to ask the reason why they differ by interchanging the order of url patterns.


以下是我的 urls.py我的 Django 应用程序 ( basic_app ) 看起来像:
from django.urls import path, re_path
from basic_app import views

app_name = 'basic_app'

urlpatterns = [
path('', views.SchoolListView.as_view(), name='list'),
re_path(r'^(?P<pk>[-\w]+)/$', views.SchoolDetailView.as_view(), name='detail'),
path('create/', views.SchoolCreateView.as_view(), name='create'),
]
当我运行服务器并输入网址时 http://127.0.0.1:8000/basic_app/create/ ,它抛出以下错误:
ValueError at /basic_app/create/
Field 'id' expected a number but got 'create'.
Request Method: GET
Request URL: http://127.0.0.1:8000/basic_app/create/
Django Version: 3.2.4
Exception Type: ValueError
Exception Value:
Field 'id' expected a number but got 'create'.

有趣的是,当我将第二个和第三个 url 模式的顺序交换如下时:
from django.urls import path, re_path
from basic_app import views

app_name = 'basic_app'

urlpatterns = [
path('', views.SchoolListView.as_view(), name='list'),
path('create/', views.SchoolCreateView.as_view(), name='create'),
re_path(r'^(?P<pk>[-\w]+)/$', views.SchoolDetailView.as_view(), name='detail'),
]
我得到了一个不同的错误:
ImproperlyConfigured at /basic_app/create/
Using ModelFormMixin (base class of SchoolCreateView) without the 'fields' attribute is prohibited.
Request Method: GET
Request URL: http://127.0.0.1:8000/basic_app/create/
Django Version: 3.2.4
Exception Type: ImproperlyConfigured
Exception Value:
Using ModelFormMixin (base class of SchoolCreateView) without the 'fields' attribute is prohibited.
我是 Django 的新手,对这个事件很好奇。为什么python不让 create/转到下一个模式进行检查,因为它不适合第一个模式?

我的 views.py文件如下所示:
from django.shortcuts import render
from django.views.generic import (
View,
TemplateView,
ListView,
DetailView,
CreateView,
UpdateView,
DeleteView
)
from . import models


# Create your views here.
class IndexView(TemplateView):
template_name = 'index.html'


class SchoolListView(ListView):
context_object_name = 'schools'
model = models.School


class SchoolDetailView(DetailView):
context_object_name = 'school_detail'
model = models.School
template_name = 'basic_app/school_detail.html'


class SchoolCreateView(CreateView):
model = models.School

最佳答案

Django 旨在匹配 url 的顶部到底部。如果您因此访问 /basic_app/create/ ,以及带有 SchoolDetailView 的规则首先,它将触发该 View ,因为 create/与正则表达式匹配 (?P<pk>[-\w+])/$ .由于您的对象的主键可能是 AutoField , 匹配 create/ 没有意义与 pk ,因为我们希望这是一个整数。
另一个问题是您的 SchoolCreateView未正确配置,您需要指定在 CreateView 中将使用哪些字段,所以我们可以通过以下方式实现:

class SchoolCreateView(CreateView):
model = models.School
fields = '__all__'
我们可以细化 re_path仅使用 int 而非任意 slug 触发的详细 View :
re_path(r'^(?P<pk>\d+)/$', views.SchoolDetailView.as_view(), name='detail'),
或使用路径转换器:
path(r'<int:pk>/', views.SchoolDetailView.as_view(), name='detail'),

关于python - 为什么当我交换 url 模式成员的顺序时,错误会有所不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68288908/

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