Django URL Dispatcher | URL Patterns

If you need to create the URL in the Django project for displaying views data, then you will have to learn Django URL Dispatcher.

In this tutorial, You will learn to create single or multiple URL patterns inside the Django project and App step by step. So you should not miss any single step. Because Each step is most important to implement in your project.

Learn also –

Function-Based View in Django

How to Create Django App

How to Create Django Project

URL Dispatcher in Django

URL Dispatcher is defined for creating URL Patterns in the default file named urls.py.This file provides a feature to create URLs for the app and map URL path expressions to the view functions.

The urls.py file remains inside the inner project folder, not inside the app folder.

URL Patterns –

  • can created inside the Djangio Project or Django App
  • Patthern can contains a single or multiple urls
  • pathern is created using the following syntax
urlpatterns = [
 path(route, view, kwargs=None, name=None),
]

Example –

In this example, you will learn how to create patterns of a single application inside a project.

File Name – urls.py

from course import views
urlpatterns = [
 path('django-tutorial/', views.django_tutorial)
]

File Name – views.py

This file remains inside the course app.

def django_tutorial(request):
  return HttpResponse("Learn Django");

URL Patterns inside Project

You have already known how to create URL patterns. Now you need to learn more about creating URL patterns of a single app & multiple apps separately inside the project.

Don’t skip this step. Because you will learn some new concepts to define URL patterns with some examples that will be very helpful for developing a big web application.

So, Let’s start from the next step –

URL Pattern for a Single App

Suppose that We have created an app with the name tutorial for creating its URL pattern inside the project

File Path – tutorial/views.py

Now, You have to define some functions inside views.py and map them with custom URLs creating inside the urls.py file

def django_tutorial(request):
  return HttpResponse("Learn Django Framework")

def pthon_tutorial(request):
  return HttpResponse("Learn Python Language")

File Name – urls.py

First of all, you have to import views file from the tutorial app then create single or multiple URL patterns according to your requirement

  • django-tutorial is mapped with django_tutorial that is define inside the views file
  • python-tutorial is mapped with python_tutorial that is defined inside the views file
  • java-tutorial is mapped with java_tutorial that is defined inside the views file
  • php-tutorial is mapped with php_tutorial that is defined inside the views file
from tutorial import views
urlpatterns = [
  path("django-tutorial/", views.django_tutorial),
  path("python-tutorial/", views.python_tutorial),
  path("java-tutorial/", views.java_tutorial),
  path("php-tutorial/", views.php-tutorial)
]

URL Patterns for Multiple Apps

I hope you have understood completely to create URL patterns for a single app. Now You should learn about multiple apps inside the project.

We have already created a tutorial app in the previous step. Now, We will create one more app with the name fees.

File Path – views/fees

def django_fees(request):
  return HttpResponse("Django language fees is only 50$")
def pthon_fees(request):
  return HttpResponse("Python language fees is only 50$")

File Name – urls.py

First of all, you have to import views file from the tutorial app then create single or multiple URL patterns according to your requirement

  • django-fees is mapped with django_fees that is define inside the views file
  • python-fees is mapped with python_fees that is defined inside the views file
  • java-fees is mapped with java_fees that is defined inside the views file
  • php-fees is mapped with php_fees that is defined inside the views file

First method

In this method, you can import the views and declare it alias name of different app views then use it with the view function inside the path().

from tutorial import views as tv
from fees import views as fv
urlpatterns = [
  path("django-tutorial/", tv.django_tutorial),
  path("python-tutorial/", tv.python_tutorial),
  path("java-tutorial/", tv.java_tutorial),
  path("php-tutorial/", tv.php-tutorial),

  path("django-fees/", fv.django_fees),
  path("python-fess/", fv.python_fees),
  path("java-fees/", fv.java_fees),
  path("php-fees/", fv.php-fees)
]

Second Method –

In this method, you can import the views function from appName.views and call it only without the views

from tutorial.views import django_tutorial
from tutorial.views import python_tutorial
from tutorial.views import java_tutorial
from tutorial.views import php_tutorial

from fees.views import django_fees
from fees.views import python_fees
from fees.views import java_fees
from fees.views import php_fees

urlpatterns = [
  path("django-tutorial/", django_tutorial),
  path("python-tutorial/", python_tutorial),
  path("java-tutorial/", java_tutorial),
  path("php-tutorial/", php-tutorial),

  path("django-fees/", django_fees),
  path("python-fess/", python_fees),
  path("java-fees/", java_fees),
  path("php-fees/", php-fees)
]

Steps to create URL Pattern

Now, you will learn completely to create & execute URL patterns step by step. So, you should not skip these steps –

1. Create Django Project & Apps

First of all, you have to create a Django project with the name of “myproject” using this command –

django-admin startproject myproject

Now, Go to the root directory of the myproject and create a tutorial app-

myproject>python manage.py startapp tutorial

Also, create another app with the name of fees

myproject>python manage.py startapp fees

After that, you will get a complete folder structure. But here only required folders & files are shown, So, don’t confused in the following structure

myproject
  |__myproject
  |    |__ settings.py
  |    |__ urls.py
  |__ tutorials
  |     |__ views.py
  |__ fees/
  |     |__ views
  |__ manage.py

2. Register Django Apps in settigs.py

Open settings.py of the inner project folder and register tutorial & fees app within INSTALLED_APPS array –

INSTALLED_APPS = [
    'tutorials', 'fees',
]

3. Create view functions inside app

Now, Create four view functions in the views.py file of a tutorial app.

File Path – tutorial/views.py

def django_tutorial(request):
  return HttpResponse("Learn Django Framework")
def pthon_tutorial(request):
  return HttpResponse("Learn Python Language")
def java_tutorial(request):
  return HttpResponse("Learn Java Language")
def php_tutorials(request):
  return HttpResponse("Learn PHP Language")

Also, create four views function in the views file of a fees app

File Path – fees/views.py

def django_fees(request):
  return HttpResponse("Django language fees is only 50$")
def pthon_fees(request):
  return HttpResponse("Python language fees is only 50$")
def java_fees(request):
  return HttpResponse("Java language fees is only 50$)
def php_fees(request):
  return HttpResponse("PHP language fees is only 50$")

4. Create URL Pattern inside Project

After that, do the following points to create URL patterns inside the project –

First, import views with alias tv from tutorial app

Then import fees with alias fv from fees app

After that, create some URL patterns within the urlpatterns = [] and map them with their relative view functions

File Name – urls.py

from tutorial import views as tv
from fees import views as fv
urlpatterns = [
  path("django-tutorial/", tv.django_tutorial),
  path("python-tutorial/", tv.python_tutorial),
  path("java-tutorial/", tv.java_tutorial),
  path("php-tutorial/", tv.php-tutorial),
  path("django-fees/", fv.django_fees),
  path("python-fess/", fv.python_fees),
  path("java-fees/", fv.java_fees),
  path("php-fees/", fv.php-fees)
]

5. Test URL Pattern Yourself

You have to run the Django project using this command if you have to display information on the view page

python manage.py runserver

After that test these URLs in your web browser one by one

http://localhost:8000/django-tutorial
http://localhost:8000/python-tutorial
http://localhost:8000/java-tutorial
http://localhost:8000/php-tutorial
http://localhost:8000/django-fees
http://localhost:8000/python-fees
http://localhost:8000/java-fees
http://localhost:8000/php-fees