Django PK 不适用于 PK 值高于 9



嗨,我是django的新手,我现在正试图弄清楚为什么主键(pk(不能正确工作,无法将我的表单更新为9以上的值。据说当前路径,tache_app/TacheUpdate/15,与这些路径都不匹配。我检查了很多次id 15,它确实存在。我的猜测是主键(pk(不适用于值超过9的情况。这是我的代码,请帮我弄清楚。我的英语应用程序不太好。我住在法国。

在我的urls.py文件中

"""Mettre les urls pour la tache app etc etc etc """
from django.urls import path 
from django.conf.urls import url
from . import views
urlpatterns = [
path('tache_ajouter/' , views.tache_ajouter , name='tache_ajouter' ),
url(r'^TacheCreate$', views.TacheCreate.as_view(), name='TacheCreate'),     
url(r'^TacheUpdate/(?P<pk>d)$', views.TacheUpdate.as_view(), name='TacheUpdate'),
url(r'^TacheDelete/(?P<pk>d)$', views.TacheDelete.as_view(), name='TacheDelete'),  
]

class TacheUpdate(UpdateView):
"""
Creation d une view afin de modifier les 
d une tache 
"""
model = Tache 
template_name = "tache_app/edition.html"
fields = "__all__"
from_class = TacheForm
success_url = reverse_lazy('tache') 

如果你需要什么,请问我。

d正则表达式部分匹配一个数字,而不是多个数字。为了匹配多个数字,您添加了一个+量词。这个+量词的意思是"一个或多个",因此d+的意思是一个或几个数字。所以你的url应该看起来像:

urlpatterns = [
path('tache_ajouter/', views.tache_ajouter, name='tache_ajouter'),
url(r'^TacheCreate$', views.TacheCreate.as_view(), name='TacheCreate'),
url(r'^TacheUpdate/(?P<pk>d+)$', views.TacheUpdate.as_view(), name='TacheUpdate'),
url(r'^TacheDelete/(?P<pk>d+)$', views.TacheDelete.as_view(), name='TacheDelete'),
]

由于您似乎在使用Django 2.x,因此最好使用path((而不是och-url((。它有一个更详细的语法,我发现这会使它更难出错。

urlpatterns = [
path('tache_ajouter/', views.tache_ajouter, name='tache_ajouter'),
path('TacheCreate', views.TacheCreate.as_view(), name='TacheCreate'),
path('TacheUpdate/<int:pk>', views.TacheUpdate.as_view(), name='TacheUpdate'),
path('TacheDelete/<int:pk>', views.TacheDelete.as_view(), name='TacheDelete'),
]

最新更新