我该如何查看“reverse”正在查找的当前urlpatterns?
我在视图中调用带有参数的反向函数,但是它却不能正常工作。有没有办法检查哪些内容存在以及为什么我的模式不起作用?
我该如何查看“reverse”正在查找的当前urlpatterns?
我在视图中调用带有参数的反向函数,但是它却不能正常工作。有没有办法检查哪些内容存在以及为什么我的模式不起作用?
django.urls
中的include
函数。这允许您在项目中列出任何一组urlpatterns。from django.views.generic import TemplateView
from rest_framework.response import Response
from rest_framework.routers import APIRootView
from rest_framework.schemas import get_schema_view
from django.urls import include
import re
# Create your views here.
class ApiRoot(APIRootView):
def get(self, request, *args, **kwargs):
urls = include('api.urls')[0].urlpatterns
response = {}
for url in urls:
match = re.search(r'[\w\/\-]+', str(url.pattern))
if not match:
continue
name = match.group()
response[name] = request.build_absolute_uri(name)
return Response(response)