如何修复django Reverse函数以与当前应用程序一起使用?

我对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-listtown-detail,但不知道为什么会这样!

This是项目的链接。请在这里帮助我。

谢谢

angeldou123 回答:如何修复django Reverse函数以与当前应用程序一起使用?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3152727.html

大家都在问