0

I have a small Django web app, with multiple applications inside them. I have used the include in the urls.py files, but whenever I reference the URLs in the HTML files they don't load. Below are my 3 urls.py files. The one I'm having an issue will specifically is the nodes url pattern in the nodes urls.py

#main urls.py

from django.contrib import admin
from django.urls import path, include
    
urlpatterns = [
    path('admin/', admin.site.urls),
    path('account/', include('account.urls')),
]

#account urls.py

from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views

app_name = 'account'

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('dashboard/', views.dashboard, name='dashboard'),
    path('nodes/', include('nodes.urls')),
]


#nodes urls.py

from django.urls import path
from . import views

app_name = 'nodes'
urlpatterns = [
    path('', views.nodes, name='nodes'),
]

This is my HTML file where I am referencing the URL pattern:

<li {% if section == 'nodes' %}class="active"{% endif %}>
     <a class="nav-link" href="{% url 'nodes' %}">Nodes</a>
</li>
14
  • You can try without the " " brackets. Just go with <a href={% url 'index' %}>Начало</a> Commented Aug 20, 2021 at 8:54
  • 1
    you aren't including your node urls in main urls file Commented Aug 20, 2021 at 9:02
  • @Henty how would I do that? I thought because the account urls.py is included in the main urls.py file then nodes urls.py file would be included? Commented Aug 20, 2021 at 9:09
  • oh yeah i missed that sorry, i think it would be something like 'account.nodes.nodes' or a shorter version to call it Commented Aug 20, 2021 at 9:13
  • @Henty where would I put this? Commented Aug 20, 2021 at 9:17

1 Answer 1

1

Due to this pattern name you set in urls file if change HTML file to this it must be fixed:

<li {% if section == 'nodes' %}class="active"{% endif %}>
   <a class="nav-link" href="{% url 'account:nodes:nodes' %}">Nodes</a>
</li>

just for tips :) if import urls file at the django shell and print list of urlspatterns you could see default name assign to your nodes path.

Sign up to request clarification or add additional context in comments.

4 Comments

will I also have to change this in the settings.py at the LOGIN_URL attribute?
@Morgan LOGIN_URL just the URL or named URL pattern where requests are redirected for login when using the login_required() decorator, this settings variable not related to your Django reverse path used at Django template tags, But if you use named URL these names must be the same.
@Morgan Did not my answer to solve your problem ? I was test your code at empty Django project and direct path correctly to nodes view. If you still have an error please edit your question with error logs
Yes it did thank you @Erfan, I was still having problems, but then I found one line of html which I hadn't changed and now its fixed. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.