URL namespace

For this discussion I have a Django environment with an app.
Topic:
Namespace
Desc:
I’m using url namespace and so far no issues until i have 1 web page with more than 1 link to other pages.
Problem outline:
I load / display console.html page. This page is basically including the base_console_ap.html that will be only used within this app and that’s why it’s not in the outer project level templates folder. The console_app.html load fine. When I select the first link {% url 'console_app:console_ivanti' %} it loads the console_ivanti.html page. When I select the 2nd link {% url 'console_app:console_msl' %} it still only loads the 1st link. If I re-order the urls.py path lines it flips it. Will load the 2nd page only and not page 1.

from this:

        path('console_app/', views.console_ivanti, name='console_ivanti'),
	path('console_app/', views.console_msl, name='console_msl'),

to this:

	path('console_app/', views.console_msl, name='console_msl'),
        path('console_app/', views.console_ivanti, name='console_ivanti'),

This is the structure and content of files concerned.

<folder structure>

enoc_project
	console_app
		templates
			console_app
				console_msl.html
				console_ivanti.html
				console.html
				base_console_ap.html
	urls.py
	views.py

urls.py

	from django.urls import include, path, re_pat
	from django.urls import reverse
	from . import views

	app_name = 'console_app'

	urlpatterns = [
	path('console_app/', views.console_ivanti, name='console_ivanti'),
	path('console_app/', views.console_msl, name='console_msl'),
	#path('console_app/', views.console_ivanti, name='console_ivanti'),
	]	

views.py

	def console_msl(request):
  		return render(request,'console_app/console_msl.html')
  

	def console_ivanti(request):
  		return render(request,'console_app/console_ivanti.html')

base_console_ap.html

	<li><a class="dropdown-item" href="{% url 'console_app:console_ivanti' %}">Ivanti</a></li>
      <li><a class="dropdown-item" href="{% url 'console_app:console_msl' %}">MSL - Master Server List</a></li>

console_msl.html

	{% extends 'console_app/base_console_ap.html' %}
	{% load static %}

	<!doctype html>
	<html lang="en">
  
  	<body>

	{% block content %}
	<!DOCTYPE html>
	<html>
    	<head>
        <title>Hello World</title>
    	</head>
    	<body>
        <p>Console Master Server List</p>
    	</body>
	</html>

	{% endblock content %}

console_ivanti.html

	{% extends 'console_app/base_console_ap.html' %}
	{% load static %}


	<!doctype html>
	<html lang="en">
  
  	<body>

	{% block content %}
	<!DOCTYPE html>
	<html>
    	<head>
        <title>Hello World</title>
    	</head>
    	<body>
        <p>Console Ivanti</p>
    	</body>
	</html>

	{% endblock content %}

The URLs router does not work that way. You have console_app/ twice, so one is overwriting the other. Think about a visitor to your site – they are going to click the link yoursite.com/console_app/ so there’s no other information there to tell the router which view it should be loading (beyond the console_app/ description), so it’s going to go with whatever the console_app/ directive has in it.

Side Note: When posting code or templates here, surround the code (or template) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep the text properly formatted.
(I’ve taken the liberty of fixing your original post for this.)

The problem here is that you’re using the same URL for two different views.

When a request is received, the URL dispatcher looks for the first URL definition that matches the incoming request, and calls the view for that definition.

See the docs at URL dispatcher | Django documentation | Django for more details.

I guess I don’t understand. All html pages are in the same app ‘console_app’ and in the same folder console_app > templates > console_app. The namespace is ‘console_app’ set in the urls.py file . The paths would be the same /console_app but point to 2 different functions in the views.py file. That also point to 2 different html pages.
Maybe u can give me an example of what u this the change may be.
I really appreciate you helping me.

Hi Ken,
I guess I don’t understand. All html pages are in the same app ‘console_app’ and in the same folder console_app > templates > console_app. The namespace is ‘console_app’ set in the urls.py file . The paths would be the same /console_app but point to 2 different functions in the views.py file. That also point to 2 different html pages.
Maybe u can give me an example of what u this the change may be.
I really appreciate you helping me.

The definition:

specifies that any request to the url “console_app/” will call views.console_ivanti.

A URL path cannot refer to two different views. A URL will call one view.

Again, read the docs for the details about how this works.

Hi Ken,
I created 2 new paths off of console_app and put the html files there and adjusted the urls.py and views.py files to reflect this as well. All works good now. Thank you. As a newbie you are a critical part of us being able to be successful. I appreciate it.

path(‘ivanti’, views.console_ivanti, name=‘console_ivanti’),

path(‘msl’, views.console_msl, name=‘console_msl’),