RaamEE
RaamEE

Reputation: 3507

Django how to include URLs from different apps into the same single extended template html using namespace

I have created a 2nd app in my django project and I am trying to create a HTML files that contains links to both the main app and the 2nd app.

I am getting this error:

Reverse for 'deploy' not found. 'deploy' is not a valid view function or pattern name.

My code is:

urls.py

from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from applications.atoll_queue_manager_fe_project.fe import views

urlpatterns = [
    path('admin/', admin.site.urls),

    path('', views.home, name='home'),
    path('create/', views.createtodo, name='createtodo'),

    # App2 - Deploy Side Branch
    path('deploy-side-branch/', include('fe2.urls')),
]

2nd app fe2/urls.py

from django.urls import path
from applications.fe2 import views

app_name = 'fe2'

urlpatterns = [
    path('', views.deploy_side_branch, name='deploy'),
]

navbar section in base.html (extended by new.html)

    <ul class="navbar-nav mr-auto">
            <li class="nav-item {{ deploy }}">
                <a class="nav-link" href="{% url 'deploy' %}">Deploy</a>
            </li>
            <li class="nav-item {{ current }}">
                <a class="nav-link" href="{% url 'currenttodos' %}">Current</a>
            </li>
            <li class="nav-item {{ completed }}">
                <a class="nav-link" href="{% url 'completedtodos' %}">Completed</a>
            </li>
            <li class="nav-item {{ create }}">
                <a class="nav-link" href="{% url 'createtodo' %}">Create</a>
            </li>
    </ul>

Upvotes: 0

Views: 569

Answers (2)

RaamEE
RaamEE

Reputation: 3507

This is the way to reference a URL from a 2nd app. The 2nd app is namespace, so using URLs from fe2/urls.py requires this line in base.html

    <ul class="navbar-nav mr-auto">
            <li class="nav-item {{ deploy }}">      <<<<<<------- Line to modify. Include namespace
                <a class="nav-link" href="{% url 'deploy' %}">Deploy</a>
            </li>

to look like this

            <li class="nav-item {{ fe2:deploy }}">

and

            <a class="nav-link" href="{% url 'fe2:deploy' %}">Deploy</a>

Upvotes: 0

Raghav Sharma
Raghav Sharma

Reputation: 195

use app-name along with name in href. Do this:

<a class="nav-link" href="{% url 'fe2:deploy' %}">Deploy</a> <!-- add <app_name>:<url_name> -->

instead of this:

<a class="nav-link" href="{% url 'deploy' %}">Deploy</a>

Upvotes: 1

Related Questions