Reputation: 83
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.
Upvotes: 1
Views: 466
Reputation: 334
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
Upvotes: 1