Django URL Patterns

 

In Django, each view needs to be mapped to a corresponding URL pattern. This is done via a Python module called URLConf(URL configuration). Every URLConf module must contain a variable urlpatterns which is a set of URL patterns to be matched against the requested URL. These patterns will be checked in sequence until the first match is found. Then the view corresponding to the first match is invoked. If no URL pattern matches, Django invokes an appropriate error handling view.

 

Now if we see our project we have created an app called gfg_site, the Python module to be used as URLConf is the value of ROOT_URLCONF in gfg_site/settings.py. By default this is set to ‘gfg_site.urls’. Every URLConf module must contain a variable urlpatterns which is a set of URL patterns to be matched against the requested URL. These patterns will be checked in sequence until the first match is found. Then the view corresponding to the first match is invoked. If no URL pattern matches, Django invokes an appropriate error handling view.

 

Here’s a sample code for post/urls.py:

 

Python3




from django.urls import path
from . import views
 
urlpatterns = [
    path('', views.home, name='home'),
]


 
 

Including other URLConf modules

 

It is a good practice to have a URLConf module for every app in Django. This module needs to be included in the root URLConf module as follows:

 

Python3




from django.contrib import admin
from django.urls import path, include
 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('post.urls'))
]


 
 

Now if head towards http://127.0.0.1:8000/ then our site will be – 

 

 

In the above example, the include statement will look into the URLpatterns list in the gfg_site_app/urls.py And then it will look into all the paths defined in the url.py file and will call the respective views function.

 

Getting started with Django

Python Django is a web framework that is used to create web applications very efficiently and quickly. Django is called a battery included framework because it contains a lot of in-built features such as Django Admin Interface, default database – SQLite3, etc. Django provides various ready-made components such as a way to handle user authentication, a management panel for your website, forms, a way to upload files, etc.

In this article, we will learn Django by creating a basic blogging web application.  

Similar Reads

Why Django Framework?

Django is a rapid web development framework that can be used to develop fully fleshed web applications in a short period of time. It’s very easy to switch databases in the Django framework. It has a built-in admin interface which makes it easy to work with it. Django is a fully functional framework that requires nothing else. It has thousands of additional packages available. It is very scalable....

Structure of Django Web Application

Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern for developing a web application....

Setting the Development Environment

It is always advisable to use a virtual environment before starting any Django project because sometimes when you are working on multiple projects you might need different versions of Django. The virtual environment creates a separate environment for different projects and you can install dependencies of each project separately. To create a virtual environment type the below command in the terminal –...

Installing Django

We can install Django using the pip command. To install this type the below command in the terminal....

Starting the Project

To initiate a project of Django on Your PC, open Terminal and Enter the following command...

Project Structure

A Django Project when initialized contains basic files by default such as manage.py, view.py, etc. A simple project structure is enough to create a single-page application. Here are the major files and their explanations. Inside the geeks_site folder ( project folder ) there will be the following files-...

Creating App

Till now we have created the Django project and now we will create an app. Django is famous for its unique and fully managed app structure. For every functionality, an app can be created like a completely independent module. To create an app we need to go to the directory containing manage.py and run the following command –...

Defining Models

...

Render Model in Django Admin

...

Django Views

...

Django URL Patterns

...

Django Templates

...

Styling Project

After creating and adding data to our models, we need to display the data to our site. Rendering data is a two-step process. First we need to create our view function and then we need to pass the data to our template. Here we will focus on the view function....

Django Forms

...

Django Projects

...