Django rest框架,试图定制可浏览的API



我有一个Django rest框架,我正在尝试自定义API的一个可浏览的API,但我似乎无法在HTML文件中引用它。

<!DOCTYPE html>
{% load rest_framework %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Second GDT Options</title>
</head>
<body>
<form action="{% url 'secondgdt' %}" method="post">
{% csrf_token %}
{% render_form serializer %}
<input type="submit" value="Save">
</form>
</body>
</html>

secondgdt/urls.py:

from django.urls import include, path
from rest_framework.routers import DefaultRouter
from .views import PointsViewSet
router = DefaultRouter()
router.register(r'secondgdt', PointsViewSet, basename='secondgdt')
urlpatterns = [
path('', include(router.urls)),
]

此代码在运行服务器时引发以下错误:

NoReverseMatch at /secondgdt/
Reverse for 'secondgdt' not found. 'secondgdt' is not a valid view function or pattern name.

设置.py

ROOT_URLCONF = 'triangulationapi.urls'

triangulationapi/uls.py


urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('', include('landingpage.urls')),
path('', include('threelocationstrian.urls')),
path('', include('find_second_gdt.urls')),
path('', include('KnownLocation.urls')),
] + staticfiles_urlpatterns()

我尝试将URL引用更改为"find_second_gdt",结果也是如此。

如果该应用程序是您的主要应用程序,请添加

ROOT_URLCONF = "secondgdt.urls"

settings.py


如果您有一个用于多个应用程序的主urls.py,请链接到该应用程序,然后从该应用程序重定向到secondgdt.urls

urlpatterns = [
url(r"^secondgdt/", include("secondgdt.urls"))
]

最新更新