惯用的Django URL名称间隔

时间:2018-07-16 21:22:05

标签: python django django-urls

TL; DR :我的代码有效,但是我真的认为这里有一些代码味道。

我目前正在处理Django项目,并尝试为API设置所有URL端点。我目前已经配置了所有URL,因此只需在URL中使用一个给定的主键即可缩短它们。

例如,要从客户的活动中获取帖子:

列表网址:

  • 标准嵌套:api/v1/clients/<client-pk>/campaigns/<campaign-pk>/posts/
  • 我自己的:api/v1/clients/campaigns/<campaign-pk>/posts/

详细网址:

  • 标准嵌套:api/v1/clients/<client-pk>/campaigns/<campaign-pk>/posts/<post-pk>/
  • 我自己的:api/v1/clients/campaigns/posts/<post-pk>/

要做到这一点,我不得不使用大量路径和许多包含项来手动设置每个模式。 为了展示我的问题,我将主要关注客户,广告系列和发布网址。

server / clients / urls.py

from .views import ClientViewset
from django.urls import include, path

from server.campaigns.urls import campaign_list_urlpatterns, campaign_detail_urlpatterns
from server.posts.urls import post_list_urlpatterns, post_detail_urlpatterns


urlpatterns = [
    path('clients/<str:pk>/', include([
        path('', client_detail, name='client-detail'),
        path('', include(campaign_list_urlpatterns)),
        path('', include(post_list_urlpatterns))
    ])),
    path('clients/', include([
        path('', client_list, name='client-list'),
        path('', include(campaign_detail_urlpatterns)),
        path('', include(post_detail_urlpatterns))
    ])),
]

server / campaigns / urls.py

from .views import CampaignViewset
from django.urls import include, path

from server.posts.urls import post_list_urlpatterns, post_detail_urlpatterns


campaign_list_urlpatterns = [
    path('campaigns/', campaign_list, name='campaign-list'),
    path('campaigns/counts/', campaign_counts, name='campaign-counts')
]

campaign_detail_urlpatterns = [
    path('campaigns/<str:pk>/', include([
        path('', campaign_detail, name='campaign-detail'),
        path('', include(post_list_urlpatterns))
    ])),
    path('campaigns/', include([
        path('', include(post_detail_urlpatterns))
    ]))
]

server / posts / urls.py

from .views import PostViewset
from django.urls import include, path


post_list_urlpatterns = [
    path('posts/', post_list, name='post-list')
]

post_detail_urlpatterns = [
    path('posts/<str:pk>/', include([
        path('', post_detail, name='post-detail'),
        path('', include(publish_date_list_urlpatterns))
    ])),
    path('posts/', include([
        path('', include(publish_date_detail_urlpatterns))
    ]))
]

所有这些终结点均按应有的方式正确返回并呈现,但两个终结点除外。通过我想完成的工作,在为客户端的帖子和广告系列的帖子设置端点时遇到了麻烦。作为反向搜索URL名称的障碍是相同的。

reverse('post-list')将搜索两种模式:

  • api/v1/clients/<client-pk>/posts/
  • api/v1/clients/campaigns/<campaigns-pk>/posts/

我需要找到某种方法来区分端点,但是我想做到这一点而又不重复端点,而做到这一点的一种可能方法是通过命名空间:

server / clients / urls.py

urlpatterns = [
    path('clients/<str:pk>/', include([
        path('', client_detail, name='client-detail'),
        path('', include(campaign_list_urlpatterns)),
        path('', include(post_list_urlpatterns, namespace='client-post-list')) # Added a namespace
    ])),
    path('clients/', include([
        path('', client_list, name='client-list'),
        path('', include(campaign_detail_urlpatterns)),
        path('', include(post_detail_urlpatterns, namespace='client-post-detail')) # Added a namespace
    ])),
]

server / campaigns / urls.py

campaign_list_urlpatterns = [
    path('campaigns/', campaign_list, name='campaign-list'),
    path('campaigns/counts/', campaign_counts, name='campaign-counts')
]

campaign_detail_urlpatterns = [
    path('campaigns/<str:pk>/', include([
        path('', campaign_detail, name='campaign-detail'),
        path('', include(post_list_urlpatterns, namespace='campaign-post-list')) # Added a namespace
    ])),
    path('campaigns/', include([
        path('', include(post_detail_urlpatterns, namespace='campaign-post-detail')) # Added a namespace
    ]))
]

server / posts / urls.py

post_list_urlpatterns = ([
    path('posts/', post_list, name='post-list')
], 'posts') # assigned 'app_name' of urlpatterns to 'posts'

post_detail_urlpatterns = ([
    path('posts/<str:pk>/', include([
        path('', post_detail, name='post-detail'),
        path('', include(publish_date_list_urlpatterns))
    ])),
    path('posts/', include([
        path('', include(publish_date_detail_urlpatterns))
    ]))
], 'posts') # assigned 'app_name' of urlpatterns to 'posts'

然后问题就变成了反向搜索,看起来真的很丑:reverse('client-post-list:post-list'),可能是臭代码造成的。

将名称空间分配给url的适当的惯用Django方法是什么,有没有办法清理和缩短名称?

0 个答案:

没有答案