0% found this document useful (0 votes)
14 views

Practical Django Applications and Multiple Applications

Django part 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Practical Django Applications and Multiple Applications

Django part 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

HT EASY LEARNING

Django Web Framework

In the previous video we created our first Django Application and displayed
“Hello World!” on the web page.
In this video we are going to see more Django basic applications practical.

1.Single Application with multiple views-


Our application can contain multiple views in views.py file. Each view is mapped
with a particular URL and each view has a different purpose.
For example- One view function takes care of user login, another is for contact or
feedback, one is taking care of listing details of a employee while another is
responsible for adding new employee details in the database.

Example 1-
Multiple Views for greetings (Good morning, Good afternoon, Good Evening)
Step 1: create a project named secondproject

Step 2: Navigate inside the secondproject folder and create application

Step 3: Add the greetingapp in settings.py INSTALLED_APPS

HT EASY LEARNING
HT EASY LEARNING

Step 4: Define views in views.py file-

In Django, the HttpResponse class is found in the django.http module.

Step 5: Define urlpatterns in urls.py file-

Step 6: Start the server


python manage.py runserver

HT EASY LEARNING
HT EASY LEARNING

2.One project with multiple applications-


In the above example we have seen multiple views in a single application, now we
want multiple application in a single project where one application greets the user
while other application displays current date and time.
Example 2-
Displaying current date and Time
Step 1: In the same project create new application named timeapp-

Always remember whenever we create a new app the first step to do is to add it in
INSTALLED_APPS list in the settings.py file.

HT EASY LEARNING
HT EASY LEARNING

Step 2: Define views in views.py of timeapp-

Line 7 uses the datetime.datetime.now() function to get the current date and
time. This function retrieves the current date and time from the server's system
clock.
Step 3: Define URL pattern for datetime view in urls.py-

Here we have to give v2 name for views of timeapp because of the conflict
between names of views which will raise an error. We can use the above approach
to solve this problem or we can also define urlpattern like this-

HT EASY LEARNING
HT EASY LEARNING

Step 4: Start the development server


python manage.py runserver

Que- Can we define multiple urlpatterns for the same view?


Yes, lets try it.

Defining URL patterns at application level instead of Project level-


When we work on a big Django project, that project may contain many
applications and each application can contain several views. If we define URLs for
these views in project level urls.py file than it will create maintenance problem
and it reduces the readability of code. Also, when using these applications
functionality in another project we have to again define URL patterns in the new
project that might be time consuming. To overcome all these problems we can

HT EASY LEARNING
HT EASY LEARNING

simply define the URLs at the application level and include that file in the project
level URLs.
Independent Development: Developers can work on app-specific URLs without
affecting the overall project URL structure. This simplifies development and testing
processes.
Easier Updates: As your application grows, adding or modifying URLs within an
app is more straightforward as you only need to update the app's urls.py file.

Example-
1.Create a new project and create a application-

Add the app in settings.py.


2.Create a view that displays a list of fruits.

HT EASY LEARNING
HT EASY LEARNING

3.Create a new urls.py file inside the application(testapp) folder

Create a new file


urls.py inside the
application folder

4. Application level urls.py- Add the following content in it

5.Update the project-level urls.py-


Within your main urls.py (usually at the project level), you use include() to include
URL patterns from another urls.py file (often located in an app directory)

HT EASY LEARNING
HT EASY LEARNING

Firstly, we have imported include method from django.urls module.


Then we have added the application level URLs by using include() method.

6.Start the Django development server-


python manage.py runserver

Main advantages-
The main advantages of defining urlpatterns at application level instead of project
level are:
1) It promotes reusability of Django Applications across multiple projects
2) Project level urls.py file will be clean and more readable

HT EASY LEARNING
HT EASY LEARNING

If you are following the course series and learning the


concepts easily, please subscribe to our YouTube
channel. This will motivate us to create more
educational and course videos and study material.

Join our growing community of tech enthusiasts!


Subscribe to our YouTube channel, where we simplify
programming languages in the easiest way possible.
Gain a deeper understanding with our clear
explanations and receive exclusive notes to enhance
your learning journey. Don't miss out on valuable
insights – hit that subscribe button now and embark on
a programming adventure with us!

Subscribe to our channel:


https://www.youtube.com/@HTEASYLEARNING

HT EASY LEARNING

You might also like