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

593
Visualizações
How to set up urls in django when there are multiple apps within the same project?

I am new to django. I am trying to setup the URLs. I have 2 apps under a project 'mysite'. In the urls.py for mysite, I have included the urls from the 2 apps (called 'login' and 'org')

mysite/urls.py

urlpatterns = [
    url(r'^login/', include('login.urls')),
    url(r'^org/', include('org.urls')),
]

login/urls.py

app_name = 'login'
urlpatterns = [
    url(r'^signup/$', login_views.signup, name='signup'),
]

org/urls.py

app_name = 'org'
urlpatterns = [
    url(r'^organizations/add/$', views.addorg, name='addorg'),
]

Having set it up this way, my site URLs become the following:

site.com/login/login/signup
site.com/org/organizations/add

I would like the URLs so that they are like the following.

site.com/login/signup
site.com/organizations/add

How do I get the 'includes' to work? Will it work if I change mysite/urls.py to the following?

urlpatterns = [
    url(r'^$', include('login.urls')),
    url(r'^$', include('org.urls')),
]

Is there a better way of setting up the URLs when there are multiple apps?

Thanks

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In the setup you describe, the login url would not be 'site.com/login/login/signup', but 'site.com/login/signup'. Where would the second 'login/' come from?

The solution you suggested,

urlpatterns = [
    url(r'^$', include('login.urls')),
    url(r'^$', include('org.urls')),
]

will generally work if there is no url that occurs identically in both apps. But it is not ideal, since all the patterns in the login urs will be unnecessarily tried for all requests.

Thus, you might want to consider removing the specific_app.urls internal prefixes, especially if those prefixes are the same for all views of the app. Leave it to whoever will use and include that app (here: mysite.urls) under which path they want to include the app.

# login/urls.py
urlpatterns = [
    # no 'login' prefix here. Includers know which app 
    # they are including and how they want do that.
    url(r'^signup/$', login_views.signup, name='signup'),  
]

# org/urls.py
urlpatterns = [
    # same: no 'org' prefix
    url(r'^add/$', views.addorg, name='addorg'),
]

# mysite.urls.py
urlpatterns = [
    url(r'^login/$', include('login.urls')),
    url(r'^organizations/$', include('org.urls')),
]

The resulting urls will be:

site.com/login/signup
site.com/organizations/add
about 4 years ago · Santiago Trujillo Relatório

0

mysite/urls.py

urlpatterns = [
    url(r'^login/', include('login.urls')),
    url(r'^org/', include('org.urls')),
]

login/urls.py

app_name = 'login'
urlpatterns = [
    url(r'^signup/$', login_views.signup, name='signup'),
]

If you put your urls in this way...

Your signup url will be site.com/login/signup The formula is - [your site domain + urls in main_project/urls.py + urls in any_app/urls.py]

So to correct your organization url - you should refactor your my_site/urls.py as -

urlpatterns = [
    url(r'^login/', include('login.urls')),
    url(r'^organizations/', include('org.urls')),
]

and in your orgs/urls.py should be

app_name = 'org'
urlpatterns = [
    url(r'^add/$', views.addorg, name='addorg'),
]

I hope it helps.

about 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