mardi 18 février 2020

How To reverse a URL with hyphens in the name?

I have a Django Application, with a file like the next one

app_something/url.py:

from django.urls import include, path, re_path
from rest_framework import routers
from . import views

slashless_router = routers.DefaultRouter(trailing_slash=True)
router = routers.DefaultRouter()

router.register(r'complex-name', views.ComplexNameModelViewSet)
router.register(r'simple', views.SimpleModelViewSet)

app_name='something'

urlpatterns = [
    path('api/', include(router.urls)),
]

It works perfect. Or that is what I used to think. Now, I'm trying to add Tests (Yep, my bad they should be there first)

BUT, now in my test_complex_name_api.py:

from django.urls import reverse

from rest_framework import status

from app_something import models, serializers

SIMPLE_MODEL_NAME_URL = reverse('something:simple-list')
COMPLEX_MODEL_NAME_URL = reverse('something:complex-name-list')

# ...
# My Tests

But, when I run my tests I got next error:

django.urls.exceptions.NoReverseMatch: Reverse for 'complex-name-list' not found. 'complex-name-list' is not a valid view function or pattern name.

SimpleModel works pretty cool. So, I think it's related to the name. But I haven't found which should be the name pattern to use? Should I rename the url or can I still use in this way and I'm only missing the part of the docs which talk about this? (I read from: https://www.django-rest-framework.org/api-guide/routers/)

Aucun commentaire:

Enregistrer un commentaire