Skip to content
Advertisement

NameError: name ‘urlpatterns’ is not defined using i18n_patterns

I have problems writing the urls for translation. According to this question I understand that it is because I have += so I need to put this = the bad thing is that I have to translate all my urls I can’t leave any outside of i18n, what can I do to include all my urls there?

from . import views
from django.urls import path
from django.conf.urls.i18n import i18n_patterns

app_name='Clientes'


urlpatterns+= i18n_patterns(
    path('',views.list_clientes,name='clientes_list'),
    path('add',views.create_clientes.as_view(),name='clientes_add'),
    path('edit/<int:pk>',views.edit_clientes.as_view(),name='clientes_edit'),
    path('<int:pk>/',views.detail_clientes.as_view(),name='clientes_detail'),
    path('delete/<int:pk>',views.eliminar_cliente.as_view(),name='clientes_delete'),
)

Advertisement

Answer

You did not define urlpatterns before. If you want to translate all paths, you use:

#           🖟 assignment
urlpatterns = i18n_patterns(
    path('',views.list_clientes,name='clientes_list'),
    path('add',views.create_clientes.as_view(),name='clientes_add'),
    path('edit/<int:pk>',views.edit_clientes.as_view(),name='clientes_edit'),
    path('<int:pk>/',views.detail_clientes.as_view(),name='clientes_detail'),
    path('delete/<int:pk>',views.eliminar_cliente.as_view(),name='clientes_delete'),
)

In the linked answer, it first defines a list, and then extends it with +=, exactly because not all url patterns are translated there. But if all patterns should be translated, you assign the result of i18n_patterns(..) to the urlpatterns.

Advertisement