【问题标题】:DRF Router issue with namespace命名空间的 DRF 路由器问题
【发布时间】:2018-10-23 20:14:58
【问题描述】:

让我的反向函数与我的 DRF 实现一起工作,我花了很长时间。这是我的 urls.py,我会假设以下反向工作:

urls.py

from django.conf.urls import url, include
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'compressors', views.CompressorViewSet, base_name='compressors')
from django.urls import include, path
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls, namespace='router')),
    url(r'^api-auth/', include('rest_framework.urls', 
namespace='rest_framework'))
]

然后

reverse('router:compressors', kwargs)

我错过了什么?我得到了一个未提供的 app_name,当添加到这个文件中时它没有帮助。有没有办法让我把 app_name 放到路由器上?

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    事实证明,很难准确地找到如何使用 basename,我没有将“-list”附加到我的反向。应该是这样的,希望对其他人有帮助:

    reverse('router:compressors-list')
    

    【讨论】:

    • 很高兴您解决了您的问题,只是想我会为django_extensions 添加一个推荐,它添加了一个管理命令,您可以运行python manage.py show_urls 来打印出您的所有网址,包括:视图类路径 (my_app.views.MyView)、实际 url 路径 (/apps/my-app/:url_kwarg) 和路由名称 (router:comporessors-list)
    • @ARJMP 谢谢家人!我已经安装了,但不知道该功能:) 会让这变得容易得多!
    猜你喜欢
    • 2012-01-25
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    相关资源
    最近更新 更多