我对Django和API的使用经验很少,最近开始开发this应用程序。
我的代码结构如下:
config/
env/
.env files
requirements/
__init__.py
...
settings/
__init__.py
base.py
local.py
__init__.py
urls.py
views.py
wsgi.py
projects/
api/
town/
apps.py
urls.py
views.py
...
core/
models/
town.py
serializers/
town_serializer.py
...
tests/
test_town.py
manage.py
这是文件project/api/town.urls.py
from django.urls import path
from . import views
app_name = 'town'
urlpatterns = [
path('town/',views.CreateTownView.as_view(),name='town-list'),path('town/<int:pk>/',views.TownViewSet.as_view(),name='town-detail')
]
还有config/settings/urls.py
的文件:
from django.contrib import admin
from django.urls import path,include
# from .views import views
urlpatterns = [
path('admin/',admin.site.urls),# path('',views.index)
path('api/',include('project.api.city.urls',namespace='city')),path('api/',include('project.api.town.urls',namespace='town'))
]
我曾经使用过localhost:8000/api/town
之类的路由,并且使用API时没有问题,并且它们工作得很好。当我尝试运行测试模块时,会出现唯一的问题。这是错误弹出的测试部分:-{
from django.db import IntegrityError
from django.test import TestCase
from project.core.models.town import Town
from django.urls import reverse
from rest_framework.test import APIClient
from rest_framework import status
import unittest
CREATE_TOWN_URL = reverse('town:town-list')
DetaIL_TOWN_URL = reverse('town:town-detail')
这是错误:
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'town-detail' with no arguments not found.
我已经在urls.py
的{{1}}的主URL中定义了名称空间,并且还在config
的URL中分别命名了town-list
和town-detail
,但不知道为什么会这样!
This是项目的链接。请在这里帮助我。
谢谢