Examples


Django URLs


URLs

  • Django URL is used to map URLs to respective view functions or classes.
  • Django's URL routing system is powerful and flexible, allows to define static and dynamic URLs, use regular expressions and namespace routes.

Django should redirect everything that comes into 'http://127.0.0.1:8000/' to myapp.urls. So we inlcude myapp.urls to myproject.urls

In Django Project, URLs are defined in the urls.py file. myproject/myproject/urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('',include('myapp.urls')), # Include myapp app URLs
    path('admin/', admin.site.urls), 
]

myapp.urls

we need to create urls.py file in the myapp folder and the view to the URL in the myproject/myapp/urls.py file:

from django.urls import path
from .import views

urlpatterns = [
    path("",views.index,name='index'), # Index page route
    path("about/",views.about,name='about'), # About page route
    path("contact/",views.contact,name='contact'), # Contact page route
]

Run Django Project

python manage.py runserver
D:\django_projects\myproject>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Django version 4.2.8, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Output

Django Project Folder Content

Prev Next