Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
What is the purpose of app_name in urls.py in Django?

When include()ing urlconf from the Django app to the project's urls.py, some kind of app's name (or namespace) should be specified as:

  • app_namespace in include((pattern_list, app_namespace), namespace=None) in main urls.py

or

  • app_name variable in app's urls.py.

Since, I guess, Django 2, the second method is the preferred one Although I copy-pasted first function signature from Django 3 documentation. But that's not the main point.

My current understanding of namespace parameter of include() is that it's what I use when using reverse().

What is the purpose of app_name in app's urls.py or app_namespace in main urls.py?
Are these exactly the same thing?
How is it used by Django?

Existing questions (and answers) I've found here explain HOW I should specify it rather than WHY.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

From https://docs.djangoproject.com/en/3.0/topics/http/urls/#introduction :

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It’s a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed. In other words, since multiple instances of a single application will share named URLs, namespaces provide a way to tell these named URLs apart.

over 4 years ago · Santiago Trujillo Relatório

0

app_name in app/urls.py and app_namespace in include((pattern_list, app_namespace), namespace=None) in main urls.py are the same referenced as application namespace which describes the name of the application that is being deployed.

One can either pass the whole app/urls.py or a string reference of app/urls.py with app_name to include()

# blog.urls
from django.urls import path

from . import views

app_name = 'blog'
urlpatterns = [
    path('', views.index(), name='index'),
    path('<int:pk>/', views.post(), name='detail'),
]
# project urls
from django.urls import include, path

urlpatterns = [
    path('', include('blog.urls')),
]

OR

a tuple of url patterns and app_namespace to include()

# project urls
from django.urls import include, path
from blog.urls import urlpatterns as blogpatterns

urlpatterns = [
    path('', include((blogpatterns, 'blog'))),
]

app_namespace will be the default application namespace when provided in include(). If app_namespace is not provided, then it will look for app_name in blog/urls.py and that will be the default namespace.

Without the app namespace, urls are added to global namespace which may lead to url conficts.

URL namespaces and included URLconfs | Term application namespace | include()

over 4 years ago · Santiago Trujillo Relatório

0

For years, we've (at Django) been skirting around the (confusing) distinction between an application name(space) and an instance namespace. We've always just recommended using instance namespace, as per the examples in the docs.

What's happened with Django 2.0 (and onwards) is they've made it so you can't use an instance namespace without also (first) using an application name. Instead of fixing code, we should update our examples to the correct usage.

The include needs to go like this:

urlpatterns += [ url('API/', include((router.urls, 'pikachu')) ]

The tendency is to include the second namespace='pikachu' instance namespace parameter as well, but that's not needed — it defaults to None and is set to 'pikachu' in this case automatically.

Generally, users want to be including an app-level URLs module explicitly setting the app_name attribute there, rather than including the router by hand.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda