Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

python - Django urls "overlapping"

I googled this problem and didn't find anything that would help.

Here comes my project structure:

-myproject
  -controlpanel
    -urls.py
    -views.py
    -templates
       -controlpanel
          -index.html
       -include
          -navbar.html

  -main
    -urls.py
    -views.py
    -templates
       -main
          -index.html
       -partials
          -navbar.html

  -myproject
    -urls.py

And this is my controlpanel urls.py file:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('servers', views.servers, name='servers'),
]

This is my main urls.py file:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('join', views.join, name='join'),
    path('rules', views.rules, name='rules'),
    path('services', views.services, name='services'),
    path('stats', views.stats, name='stats'),
    path('gdpr', views.gdpr, name='gdpr'),
    path('login', views.login, name='login'),
]

This is myproject urls.py:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('main.urls')),
    path('controlpanel/', include('controlpanel.urls')),
    path('admin/', admin.site.urls),
]

Now when a user doesn't specify any subdirectory he should be redirected to index.html in app main.

The problem is that some {% url 'urlname' %} return urls from other projects. For instance when i used {% url 'index' %} in main apps navbar it used url controlpanel/index which it isn't supposed to do.

This also happened to me when I was creating a navbar for controlpanel and imported CSS but I solved it by renaming the folder to "include". I would generally just rename files to fix it to something like index > home, etc.. but this app is supposed to be copied to existing projects and I don't want to do it in this dirty way.

I don't know how to fix it. Any help would be appreciated.

question from:https://stackoverflow.com/questions/66052562/django-urls-overlapping

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you might have suspected Django can tackle this problem easily, you can see some examples for this in the Django documentation

In every urls.py file you can specify an 'app_name'. In your case you could do something the following:

from django.urls import path
from . import views

app_name = 'controlpanel'
urlpatterns = [
    path('', views.index, name='index'),
    path('servers', views.servers, name='servers'),
]

Second urls.py

from django.urls import path
from . import views

app_name = 'main'
urlpatterns = [
    path('', views.index, name='index'),
    path('join', views.join, name='join'),
    path('rules', views.rules, name='rules'),
    ...
]

now in any reverse or url template tag you can include the app name like this:

{% url 'main:index' %}
{% url 'controlpanel:index' %}

Django will now know where to look for the 'name' index


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...