Django resolve()无法解析有效路径

时间:2018-08-01 23:24:59

标签: python django django-rest-framework

我想在Django中运行测试,我有一个非常简单的测试用例,旨在测试url是否可访问。我使用的是有效路径,但仍然出现 Resolver404 错误。我不知道这是否与多语言设置有关。

这是我的主要urls.py

from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
from django.urls import path, include
from rest_framework.authtoken import views
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = i18n_patterns(
    path('api-token-auth/', views.obtain_auth_token),
    path('', include('FrontEndApp.urls')),
    path('admin/', admin.site.urls),
    path('api-auth/', include('rest_framework.urls')),
    path('rosetta/', include('rosetta.urls')),
    path('general/', include('GeneralApp.urls')),
    #path('invoices_manager/', include('InvoicesManagerApp.urls')),
    path('operations_manager/', include('OperationsManagerApp.urls')),
    #path('payments_manager/', include('PaymentsManagerApp.urls')),
    #path('providers_manager/', include('ProvidersManagerApp.urls')),
    path('rates_manager/', include('RatesManagerApp.urls')),
    #path('reports_manager/', include('ReportsManagerApp.urls')),
    path('reservations_manager/', include('ReservationsManagerApp.urls')),
    path('users_manager/', include('UsersManagerApp.urls'))
)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的应用urls.py

from django.urls import path, include
from GeneralApp import views, models

from rest_framework import routers, renderers

APP_NAME = 'GeneralApp'
urlpatterns = [
    path('', include(router.urls)),
    path('catalogs/', views.CatalogViewSet.as_view({'get':'list'})),
    path('countries/', countries_list, name='rest_countries_list'),
    path('countries/<int:pk>/', country_detail, name='rest_country_detail'),
    path('countries/<int:pk>/states/', states_list, name='rest_country_states_list'),
    path('states/', states_list, name='rest_states_list'),
    path('states/<int:pk>/', state_detail, name='rest_state_detail'),
    path('states/<int:pk>/cities/', cities_list, name='rest_state_cities_list'),
    path('cities/', cities_list, name='rest_cities_list'),
    path('cities/<int:pk>/', city_detail, name='rest_city_detail'),
    path('cities/<int:pk_city>/zones/', city_zones_list, name='rest_city_zones_list'),
    path('zones/<int:pk>/', zone_detail, name='rest_zone_detail'),
    path('zones/', zones_list, name='rest_zones_list'),
    path('airports/<int:pk_city>/airports/', city_airports_list, name='rest_city_airports_list'),
    path('airports/', airports_list, name='rest_airports_list'),
    path('airports/<int:pk>/', airport_detail, name='rest_airport_detail'),
    path('languages/', languages_list, name='rest_languages_list'),
    path('languages/<int:pk>/', language_detail, name='rest_language_detail'),
    path('provider_types/', provider_types_list, name='rest_provider_types_list'),
    path('provider_types/<int:pk>/', provider_type_detail, name='rest_provider_type_detail'),
    path('companies/', companies_list, name='rest_companies_list'),
    path('companies/<int:pk>/', company_detail, name='rest_company_detail'),
    path('companies/<int:pk>/provider/', provider_detail, name='rest_provider_detail'),
    path('providers/', providers_list, name='rest_providers_list'),
    path('providers/<int:pk>/', provider_detail, name='rest_provider_detail'),
    path('company_providers/', company_providers_list, name='rest_company_providers_list'),
    path('company_providers/<int:pk>/', company_provider_detail, name='rest_company_provider_detail'),
    path('contacts_hlkd/', contacts_hlkd_list, name='rest_contacts_hlkd_list'),
    path('contacts_hlkd/<int:pk>/', contact_hlkd_detail, name='rest_contact_hlkd_detail'),
    path('contacts/', contacts_list, name='rest_contacts_list'),
    path('contacts/<int:pk>/', contact_detail, name='rest_contact_detail'),
    path('exchange_rates/', exchange_rates_list, name='rest_exchange_rates_list'),
    path('exchange_rates/<int:pk>/', exchange_rate_detail, name='rest_exchage_rate_detail'),
    path('zipcodes/', zipcodes_list, name='rest_zipcodes_list'),
    path('zipcodes/<int:pk>/', zipcode_detail, name='rest_zipcode_detail'),
    path('zipcodes/<int:pk>/cities/', cities_list, name='rest_cities_list'),
    path('zipcodes/<int:pk>/states/', states_list, name='rest_states_list'),
    path('zipcodes/<int:pk>/contries/', countries_list, name='rest_countries_list'),
    path('markets/', markets_list, name='rest_markets_list'),
    path('markets/<int:pk>/', market_detail, name='rest_market_detail'),
    path('parameters/', parameters_list, name='rest_parameters_list'),
    path('parameters/<int:pk>/', parameter_detail, name='rest_parameter_detail'),
]

这是我的tests.py

from django.test import TestCase
from django.urls import resolve
from GeneralApp.views import CountryViewSet

# Create your tests here.
class CountryEndpointTest(TestCase):
    def test_url_resolves_to_view(self):
        found = resolve('/general/countries/')
        self.assertEqual(found.func, views.CountryViewSet)

这是我得到的错误:

File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/
urls/resolvers.py", line 523, in resolve
    raise Resolver404({'tried': tried, 'path': new_path})
django.urls.exceptions.Resolver404: {'tried': [[<URLResolver <URLPattern list> (None:None) 'en-us/'>]], 'path': 'ge
neral/countries/'}

1 个答案:

答案 0 :(得分:0)

prefix_default_language=False传递到i18n_patterns函数。如果没有,则必须在所有网址格式中使用语言代码。

https://docs.djangoproject.com/en/2.0/topics/i18n/translation/#django.conf.urls.i18n.i18n_patterns

urlpatterns = i18n_patterns(
  path('api-token-auth/', views.obtain_auth_token),
  ...
  prefix_default_language=False
)