How to Create Multiple Views in Django?

Hi Dev,
in this article will provide some of the most important example how to create multiple views in django. let’s talk about how to split views into multiple files in django. if you have question about django split views into multiple files then I will give simple example with solution. I would like to share with you django split views into multiple files. Let's see bellow example multiple views in django.
Most of the time, you can develop separate modules to perform processing work outside of views, import them, and then use their functions inside views.
You can sometimes divide the job among several apps. Obviously, when it makes sense. But occasionally, the number of views will increase. You might want to separate the views into separate files in this situation.
Here i explained simply step by step example of how to create multiple views in django.
Step 1: Create a Project
In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:
django-admin startproject example
Step 2: Create a App
Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.
python manage.py startapp core
Step 3: App Dir
Our app dir structure look like this:
|∙∙core/ |∙∙__init__.py |∙∙admin.py |∙∙migrations/ |∙∙models.py |∙∙tests.py |∙∙urls.py |∙∙views.pyStep 4: Creating the Views
In this step, we need to configure four different views open the core/views.py file and add:
core/views.pyfrom django.shortcuts import render def view_1(request): return render(request, 'view_1.html') def view_2(request): return render(request, 'view_2.html') def view_3(request): return render(request, 'view_3.html') def view_4(request): return render(request, 'view_4.html')Step 5: Creating URLs
In this step, we need to configure four different urls Create core/urls.py with your text editor and paste below code.
core/urls.pyfrom django.urls import re_path import .views urlpatterns = [ re_path(r'^aaa$', views.view_1, name='view_1'), re_path(r'^bbb$', views.view_2, name='view_2'), re_path(r'^ccc$', views.view_3, name='view_3'), re_path(r'^ddd$', views.view_4, name='view_4'), ]
Step 6: Splitting Views
If you are reworking your code base, this approach is good. You won't have to alter the way you manage your urls.py this way. The view functions are still accessible through views.view_a a even though they are in separate files.
Delete the views.py file and create a directory named views. Add a __init__.py file inside it and create the separated view files.
|∙∙core/ |∙∙__init__.py |∙∙admin.py |∙∙migrations/ |∙∙models.py |∙∙tests.py |∙∙urls.py |∙∙views/ |∙∙__init__.py |∙∙one.py |∙∙two.pyviews/__init__.py
Importing every module contained inside of each view file is a crucial step.
from .one import * from .two import *views/one.py
from django.shortcuts import render def view_a(request): return render(request, 'view_1.html') def view_b(request): return render(request, 'view_2.html')views/two.py
from django.shortcuts import render def view_c(request): return render(request, 'view_3.html') def view_d(request): return render(request, 'view_4.html')
Nothing needs to be changed in this area; you can just import the views from the various files directly. The location of the view function is actually irrelevant to Django. Likewise, the name views is not required but is advised.
The example below you may keep views/__init__.py empty, since we are importing the views directly.
from django.urls import re_path from .views import one from .views import two urlpatterns = [ re_path(r'^aaa$', one.view_1, name='view_1'), re_path(r'^bbb$', one.view_2, name='view_2'), re_path(r'^ccc$', two.view_3, name='view_3'), re_path(r'^ddd$', two.view_4, name='view_4'), ]
I Hope It will help you....