Unit-4 - Connecting SQLite with Django
Unit-4 - Connecting SQLite with Django
Unit: IV
Meghali Das
Course Details
(Asst. Professor)
(B. Tech. 5th Sem)
CSE Department
01/26/2025 3
Meghali Das Python web development with Django Unit IV
Syllabus
Analyzing and creating a functional website in Django and deploy Django Web
Application on Cloud.
CO2 : Demonstrate web application framework (Django) to design and implement dynamic website
CO3 : Implementing and analyzing the concept of Integrating Accounts & Authentication on Django
CO4 : Understand the impact of web designing by database connectivity with SQLite
CO5 : Analyzing & Creating a functional website in Django and deploy Django Web Application Cloud
PO8 : Ethics
PO10 : Communication
CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 2 2 2 3 3 - - - - - - -
CO2 3 2 3 2 3 - - - - - - -
CO3 3 2 3 2 3 - - - - - - -
CO4 3 2 3 2 3 - - - - - - -
CO5 3 2 3 3 3 - - - - - - -
CO1 3 - - -
CO2 3 3 - -
CO3 3 3 - -
CO4 3 3 - -
CO5 3 3 - -
Program Educational
PEOs Description
Objectives (PEOs)
To have an excellent scientific and engineering breadth so as to comprehend,
PEOs analyze, design and provide sustainable solutions for real-life problems using state-
of-the-art technologies.
To have life-long learning for up-skilling and re-skilling for successful professional
PEOs career as engineer, scientist, entrepreneur and bureaucrat for betterment of society.
• https://youtu.be/tA42nHmmEKw?list=PLh2mXjKcTPSACrQxPM2_1Ojus5HX88ht7
• https://youtu.be/8ndsDXohLMQ?list=PLDsnL5pk7-N_9oy2RN4A65Z-PEnvtc7rf
• https://youtu.be/QXeEoD0pB3E?list=PLsyeobzWxl7poL9JTVyndKe62ieoN-MZ3
• https://youtu.be/9MmC_uGjBsM?list=PL3pGy4HtqwD02GVgM96-V0sq4_DSinqvf
• Database Migrations.
• Fetch Data From Database.
• Displaying Data On Templates.
• Adding Condition On Data.
• Sending data from url to view.
• Sending data from view to template.
• Saving objects into database, Sorting objects, Filtering objects, Deleting objects.
• Difference between session and cookie, Creating sessions and cookies in
Django.
Database migration is the process of migrating data from one or more source databases
to one or more target databases by using a database migration service.
When a migration is finished, the dataset in the source databases resides fully, though
possibly restructured, in the target databases. Clients that accessed the source
databases are then switched over to the target databases, and the source databases are
turned down.
A database migration service runs within Google Cloud and accesses both source and
target databases. Two variants are represented: (a) shows the migration from a source
database in an on-premises data center or a remote cloud to a managed database like
Cloud Spanner; (b) shows a migration to a database on Compute Engine
01/26/2025 Meghali Das Python web development with Django Unit IV 28
Database Migrations
Even though the target databases are different in type (managed and unmanaged) and setup,
the 01/26/2025
database migration architecture
Meghali Das andPython
configuration
web development withis the same
Django Unit IV for both cases. 31
Database Migrations
Terminology :-
Terminology :-
A database migration architecture describes the various components required for executing a
database migration. This section introduces a generic deployment architecture and treats the
database migration system as a separate component. It also discusses the features of a
database management system that support data migration as well as non-functional
properties that are important for many use cases.
Deployment architecture:-
A database migration can occur between source and target databases located in any
environment, like on-premises or different clouds. Each source and target database can be in
a different environment; it is not necessary that all are collocated in the same environment.
01/26/2025 Meghali Das Python web development with Django Unit IV 34
Database Migrations
In django, we retrieve the data in the views.py file, where we write our
functions.
To retrieve data from database, first we have to create a url for that. Open the
urls.py file inside the application folder, and create a new path as shown
below:
path('students/', views.students, name="students"),
Now create a function in the views.py file, with the name "students" as shown in
Here, we have created a variable form and we have called all the objects of the Student
model. Objects is nothing but the records in the database. Then we have created a
dictionary named context and passed the form variable in the dictionary and passed
that dictionary in the render() function.
As we have mentioned in the render(), let us create a .html file with the name
index.html and we will be displaying the all the data in the index.html. To display the
data, we will be using the for loop as there will be multiple data in our database.
01/26/2025 Meghali Das Python web development with Django Unit IV 37
Fetch Data From Database
This will print all the data that we had fetched and
stored in the form variable.
01/26/2025 Meghali Das Python web development with Django Unit IV 38
Displaying Data On Templates
Django templates
In a Django template, you have to close the if template tag. You can write
the condition in an if template tag. Inside the block, you can write the
statements or the HTML code that you want to render if the condition
returns a True value.
I have created an example where I will enter two numbers and compare
them inside a Django template.
The following is the Django template where I will submit the two numbers:
In Django, you can also pass parameters as part of the URL. In this Django tutorial, you will
learn how to get URL parameters in Django.
In various web applications, you might have seen some URLs that end with some
parameters. For example, look at the below URL:
https://www.shop.tsinfo.com/products/12
The above URL fetches the products page corresponding to a product id i.e. 12. This means
12 is a URL parameter and the result is shown according to this value.
To get a parameter from the URL, you have to perform the steps explained below:
1. Create and map a path to a view in the application’s URLs file and pass the
parameters to the view
2. Define a function in the view that will take the parameter and pass the parameters to
Django template.
3. Design the Django template to show the results corresponding the passed
parameters.
You can pass a URL parameter from the URL to a view using a path converter.
But, firstly you have to create a path and map it to a view.
For this, you have to edit your application’s urls.py file.
A sample urls.py file will look like this:
https://www.shop.tsinfo.com/products/12
We will talk about passing dynamic data to templates for rendering it.
Let's write a simple view that takes user information such as first name,
last name and address and renders it in the template.
Cookies, technically called HTTP Cookies are small text files which are created and
maintained by your browser on the particular request of Web-Server. They are stored
locally by your browser, and most browser will also show you the cookies generated
in the Privacy and Security settings of the browser.
HTTP is a stateless protocol. When any request is sent to the server, over this protocol,
the server cannot distinguish whether the user is new or has visited the site
previously.
Suppose, you are logging in any website, that website will respond the browser with
some cookies which will have some unique identification of user generated by the
server and some more details according to the context of the website.
Cookies made these implementations possible with ease which were previously not
possible over HTTP implementation.
The browser repeats the process until the cookie expires or the session is closed and
the cookie is deleted by the browser itself.
Then, the cookie applies in all sorts of tasks, like when your login to a website or
when shopping online on the web. Google AdSense and Google Analytics can also
track you using the cookies they generate. Different websites use cookies differently
according to their needs.
01/26/2025 Meghali Das Python web development with Django Unit IV 58
Session and Cookie
What are Sessions?
After observing these problems of cookies, the web-developers came with a new and more
secure concept, Sessions.
The session is a semi-permanent and two-way communication between the server and the
browser.
Let’s understand this technical definition in detail. Here semi means that session will exist until
the user logs out or closes the browser. The two-way communication means that every time the
browser/client makes a request, the server receives the request and cookies containing specific
parameters and a unique Session ID which the server generates to identify the user. The Session
ID doesn’t change for a particular session, but the website generates it every time a new
session starts.
Generally, Important Session Cookies containing these Session IDs deletes when the session
ends. But, this won’t have any effect on the cookies which have fix expire time.
Making and generating sessions securely can be a hefty task, and now we will look at Django’s
implementation of the same.
Django bypasses lots of work which otherwise would be required when working
on cookies. Django has methods like set_cookie() which we can use to create
cookies very easily.
The set_cookie() has these attributes:
Django allows you to easily create session variables and manipulate them accordingly.
The request object in Django has a session attribute, which creates, access and edits the
session variables. This attribute acts like a dictionary, i.e., you can define the session names
as keys and their value as values.
Step 1. We will start by editing our views.py file. Add this section of code.
• https://youtu.be/tA42nHmmEKw?list=PLh2mXjKcTPSACrQxPM2_1Ojus5HX88ht7
• https://youtu.be/8ndsDXohLMQ?list=PLDsnL5pk7-N_9oy2RN4A65Z-PEnvtc7rf
• https://youtu.be/QXeEoD0pB3E?list=PLsyeobzWxl7poL9JTVyndKe62ieoN-MZ3
• https://youtu.be/9MmC_uGjBsM?list=PL3pGy4HtqwD02GVgM96-V0sq4_DSinqvf
8. What is the OUTPUT of the following Statement? 9. What is the OUTPUT when the following
print 0xA + 0xB + 0xC : Statement is executed?
0xA0xB0xC “abc”+”xyz”
33 abc
ABC abcxyz
000XXXABC abcz
abcxy
(1) Tom Aratyn, “Building Django 2.0 Web Applications: Create enterprise-grade, scalable
Python web applications easily with Django 2.0”, 2nd Edition 2018, Packt Publishing.
(2) Nigel George, “Build a website with Django”, 1st Edition 2019, GNW Independent
Publishing Edition.
(3) Ray Yao,” Django in 8 Hours: For Beginners, Learn Coding Fast!, 2nd Edition 2020,
Independently published Edition.
(4) Harry Percival, “Test-Driven Development with Python: Obey the Testing Goat: Using
Django, Selenium, and JavaScript”, 2nd Edition 2019, Kindle Edition.