如何使用 uuid 作为 slug 访问 Django UpdateView

How to access a Django UpdateView with using a uuid as slug

我想知道如何使用 uuid 作为 slug 来访问 UpdateView。所以我的目标是URL喜欢

http://myapp.example.de/edit/7efdbb03-bdcf-4b8a-85dd-eb96ac9954fd

我定义了这样的视图:

class DeviceEventUploadView(UpdateView):

    model = Event
    slug_url_kwarg = 'uuid_slug'
    slug_field = 'unique_id' 

和urls.py像这样:


urlpatterns = [
    path('admin/', admin.site.urls),
    path('edit/<uuid:uuid_slug>',
        DeviceEventUploadView.as_view(),
        name='event_update'),
]

我得到:


Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:

    admin/
    ^edit/<uuid:uuid_slug> [name='event_update']

The current path, edit/7efdbb03-bdcf-4b8a-85dd-eb96ac9954fd/, didn’t match any of these.

我的思维失败在哪里?

您忘记关闭 <uuid:uuid_slug>。它需要一个右尖括号 >。此外,这是 path 语法。因此,您将其定义为:

<strong>path</strong>(
    'edit/<strong><uuid:uuid_slug></strong>/',
    DeviceEventUploadView.as_view(),
    name='event_update'
),