Tuesday, April 22, 2025

Django URL Routing, Navigation

We will create new html files to have something to work with: contact.html, about.html

Of course, they will be in templates directory.

Then we need to fix views.py


from django.shortcuts import render

def home(request):
    return render(request, 'home.html', {})

def about(request):
    return render(request, 'about.html', {})

def contact(request):
    return render(request, 'contact.html', {})

We need to fix local urls.py for new routes:


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('about/', views.about, name = 'about'),
    path('contact/', views.contact, name = 'contact'),
]

Now, let's check those pages. We will visit links directly in address bar of browser, because there's no navigation in place - yet.

  • http://127.0.0.1:8000
  • http://127.0.0.1:8000/about
  • http://127.0.0.1:8000/contact

C:\my_projects>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 26, 2020 - 14:07:27
Django version 3.0.8, using settings 'my_projects.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[26/Jul/2020 14:07:34] "GET / HTTP/1.1" 200 14
[26/Jul/2020 14:07:41] "GET /about/ HTTP/1.1" 200 15
[26/Jul/2020 14:07:48] "GET /contact/ HTTP/1.1" 200 17

Ok, let's add navigation to all pages. We will hardcode for now:


<a href="">Home</a> | <a href="about/">About</a> | <a href="contact/">Contact</a> |

Actually, that navigation will partially work - we will fix it in next tutorial on dynamic navigation.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...