在Django中,如何将模块的url配置封装在模块内部? [英] In Django, how do you keep a module's url configurations encapsulated inside the module?
问题描述
当前,我使用以下命令将URL配置导入到我的Django项目中:
Currently, I import url configurations into my Django project with:
from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_path
from rest_framework import routers
from greeter.views import GreeterViewSet
ROUTER = routers.DefaultRouter()
ROUTER.register(r'greeters', GreeterViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include(ROUTER.urls)),
]
有没有一种方法可以移动代码的这些部分:
Is there a way where I can move these parts of the code:
ROUTER = routers.DefaultRouter()
ROUTER.register(r'greeters', GreeterViewSet)
放入 greeter/urls.py
中的单独文件?
并仍然保留以下网址:
- 获取/greeter/以获取问候者列表
- POST/greeter/创建一个新的问候者
我尝试过:
my_project/urls.py
my_project/urls.py
from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^greeters/', include('greeter.urls')),
]
greeter/urls.py
greeter/urls.py
from django.conf.urls import url, include
from rest_framework import routers
from .views import GreeterViewSet
ROUTER = routers.DefaultRouter()
ROUTER.register(r'^', GreeterViewSet)
urlpatterns = [
url(r'^', include(ROUTER.urls)),
]
但是得到了:
$ curl -H 'Accept: application/vnd.api+json; indent=2' -X POST http://localhost:8000/greeters/
{
"errors": [
{
"detail": "Method \"POST\" not allowed.",
"source": {
"pointer": "/data"
},
"status": "405"
}
]
}
以防万一,这是我原来的问题已解决:方法"POST"Django Rest Framework不允许使用
In case it helps, here is my original question that has been resolved: Method "POST" not allowed with Django Rest Framework
更新:
借助答案,我得以得出以下解决方案:
With the help of the answers, I was able to arrive at this solution:
my_project/urls.py
my_project/urls.py
from django.conf.urls import include
from django.contrib import admin
from django.urls import re_path
urlpatterns = [
re_path('admin/', admin.site.urls),
re_path('greeters/', include('greeter.urls')),
]
greeter/urls.py
greeter/urls.py
from django.conf.urls import include
from django.urls import re_path
from rest_framework import routers
from .views import GreeterViewSet
ROUTER = routers.DefaultRouter()
ROUTER.register(r'', GreeterViewSet)
urlpatterns = [
re_path(r'', include(ROUTER.urls)),
]
与此:
- 所有迎接者网址配置均保存在迎接者内部模块
- 项目url配置将"greeter"资源与迎宾模块
- 似乎可以将其他问候者视图添加到迎宾者网址配置(我尚未对此进行测试)
我认为这是我所能得到的.再次感谢您提供的所有帮助:)
I think this is as good as I can get it. Thanks again for all of the help :)
推荐答案
正如我之前对您所说的,路由器需要一个前缀.您需要从主要网址中删除该前缀,并在路由器本身中使用它.
As I said to you before, the Router needs a prefix. You need to remove that prefix from your main URLs and use it in the router itself.
主要:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('greeter.urls')),
]
app:
ROUTER = routers.DefaultRouter()
ROUTER.register(r'^greeter/', GreeterViewSet)
urlpatterns = ROUTER.urls
(由于路由器路由器没有其他URL,因此不需要在其中使用include,您可以直接使用路由器URL.)
(Since you don't have any URLs other than the router ones, you don't need to use include there, you can just use the router urls directly.)
还请注意,这几乎可以肯定不是您想要做的全部;这意味着除了视图集的URL之外,您再也没有其他任何URL.
Also note, this whole thing is almost certainly not what you want to do; it means you can never have any URLs other than those for your viewset.
这篇关于在Django中,如何将模块的url配置封装在模块内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!