简体   繁体   English

如何为 Django 管理员正确设置带有“admin/”的自定义 url?

[英]How to set a custom url with "admin/" properly for Django Admin?

I set the custom url "admin/success/" with "admin/" as shown below:我将自定义 url "admin/success/"设置为"admin/"如下图所示:

# "core/urls.py"

urlpatterns = [
    path("admin/", admin.site.urls),
    path("admin/success/", success, name='success'), # Here
]

And, this is "views.py" :而且,这是“views.py”

# "core/views.py"

from django.http import HttpResponse

def success(request):
    return HttpResponse("Hello World")

Then, I got "Page not found (404)" error as shown below:然后,出现“找不到页面(404)”错误,如下所示:

在此处输入图像描述

But, when I removed "admin/" :但是,当我删除"admin/"时:

# "core/urls.py"

urlpatterns = [
    # path("admin/", admin.site.urls), # Here
    path("admin/success/", success, name='success'),
]

The custom url is displayed properly as shown below:自定义url正常显示如下图:

在此处输入图像描述

So, are there any ways to properly set the custom url "admin/success/" with "admin/" so that the custom url is displayed properly?那么,有什么方法可以正确设置自定义 url “admin/success/”“admin/” ,以便正确显示自定义 url

In your urls.py just change the arrangement of /admin and /admin/succes .在您的urls.py中,只需更改/admin/admin/succes的排列。 Because the arrangement matter the last url must be the default one.因为安排很重要,最后一个 url 必须是默认的。

For example if in your app you have several endpoints, you have to place the default empty endpoint the last one as Django checks all until reaches the default, If you place the empty one first it won't see the others.例如,如果在您的app中有多个端点,则必须将默认的空端点放置在最后一个端点,因为 Django 会检查所有端点,直到达到默认值,如果将空端点放在第一位,它将看不到其他端点。

urlpatterns = [
    path("admin/success/", success, name='success'), # Here
    path("admin/", admin.site.urls),
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM