Prince Django

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

WDPD

Practical
File

Submitted To: Submitted By:


Mr. Praveen Thakur Prince Michael
03825502021
BCA 4th Sem 1st shift
CERTIFICATE

This is to certify that Web Development with Python Django Practical


File being submitted is a bonafide work done by Prince Michael
Student of BCA 4th Sem Ist shift in partial fulfillment of the award of
the BCA degree.
He has worked under my guidance and supervision and has fulfilled
the requirements of the file that has been reached the requisite
standards.

Date: 08th June, 2023

Mr Praveen Thakur

(IT Professor)
ACKNOWLEDGEMENT

I Nitin Saini, the student of BCA 4th Sem, am extremely grateful to JIMS College for the
confidence bestowed in me and entrusting my Practical file of Web Development with
Python.

At this juncture I feel deeply honored in expressing my sincere thanks to my faculty,

Mr Praveen Thakur for his valuable inputs, his guidance, encouragement and whole-
hearted cooperation and constructive criticism throughout the preparation of this practical
file.

I take this opportunity to thank all the lab assistants, coordinators who have directly or
indirectly without which this file would not have been possible. Lastly but not the least I
place deep sense of gratitude to my family members and my friends who have been constant
source of inspiration during the preparation of this Practical file
INDEX
S.NO. PRACTICAL PG SIGN.

1. Install Python including installation of pip, installation and setting up


virtual environment, installation of Django

2. Create Django Project

3. Create Django Project to display “Hello World !!” in a web page

4. Create Django Project to display “Hello World !!” in a web page using a
Template. Render the template to display a webpage.

5. Design a Base Template Class and Child template class with following
features to demonstrate Django Template Inheritance support.

6. Create following Django Form to accept user data and print same in the
VS code terminal.

7. Create following Django Form to accept user data to create SQLite


database Table and update.
Practical

-19 : Install Python including installation of pip, installation and setting up virtual
environment, installation of Django

(a) Steps Followed


a. Installation screen of Python version installed
 To install Python, I downloaded the Python installer for my operating system from the official
Python website (https://www.python.org/downloads/). I chose the latest stable version,
which at the time of writing was Python 3.9.6. The installation process varied depending on
the operating system, but I followed the instructions provided by the installer.

b. Command to Install Virtual Environment


 Once Python was installed, I opened the command prompt or terminal and used the following
command to install the virtual environment package called "virtualenv" using pip:

 After the virtualenv package was installed, I created a new virtual environment for my Django
project using the following command:

This command created a new directory called "myenv" and installed a standalone Python
environment within it.

To activate the virtual environment, I used the appropriate command based on my operating
system:
For Windows:
c. Command to Install Django
 Once the virtual environment was activated, I installed Django using pip with the following
command:

This command downloaded and installed the latest stable version of Django.
To verify that Django was successfully installed, I ran the following command:

d. Command to check Succesful Django Installation

(b) Discussion
a. Why Virtual Environment was setup
 Virtual environment was set up to create an isolated Python environment for our Django
project. It allows us to have separate Python and package installations for different projects,
ensuring that the dependencies of one project do not interfere with another. It helps in
maintaining project-specific dependencies and avoiding version conflicts.
(c) Output
a. ScreenShot of Successful Django Installation
Practical -

2 : Create Django Project

(a) Steps Followed


a. Command to create Django Project
 To create a Django project, I used the following command:

(b) Discussion
a. What is the purpose of django-admin and manage.py
 django-admin: It is a command-line tool provided by Django that allows you to perform
various administrative tasks related to Django projects. It is used to create a new Django
project, start the development server, run management commands, and more.
 manage.py: It is an automatically generated Python script within the project directory. It acts
as a command-line interface to interact with the Django project. It provides shortcuts for
common tasks like running the development server, applying database migrations, running
tests, and executing management commands specific to the project.

b. List and explain fields found in settings.py


 settings.py: It is a configuration file located inside the project directory. It contains various
settings that define the behavior and characteristics of the Django project. Some important
fields found in settings.py are:
DEBUG: A boolean flag that determines whether the project runs in debug mode. Debug mode
provides detailed error pages and enables additional development features. In production,
this should be set to False.
ALLOWED_HOSTS: A list of strings representing the host/domain names that this Django
project can serve. Only requests from these hosts are allowed. It is a security measure to
prevent unauthorized access.
INSTALLED_APPS: A list of strings specifying the names of all the applications (both built-in
and custom) that are installed and active in the project. Each application can provide models,
views, templates, and other functionalities.
DATABASES: A dictionary containing the database configuration for the project. It includes
details such as database engine, name, user credentials, host, and port.
STATIC_URL and STATIC_ROOT: These settings are used for handling static files in the project.
STATIC_URL specifies the base URL for serving static files, and STATIC_ROOT is the absolute
path to the directory where static files will be collected for deployment.

(c) Output
a. ScreenShot of django Project folder structure.

This is the initial structure of a Django project, where myproject/ is the root directory containing
the project files and manage.py is the management script.
Practical -

3 : Create Django Project to display “ Hello World !! ” in a web page

(a) Steps Followed


a. Code Changes in views.py
 In the views.py file, I added the following code:

This code defines a view function named hello_world that takes a request object and returns
an HTTP response with the content "Hello World!!".

b. Code Changes in urls.py


 In the urls.py file, I modified it as follows:

This code imports the hello_world function from the views module and adds a URL pattern
that maps the root URL to the hello_world view.

c. Code Changes in ROOT_URLCONF


 In the settings.py file, I ensured that the ROOT_URLCONF field is set to the correct value:

This code specifies the Python import path to the project's URL configuration. In this case, it is set
to 'myproject.urls', which is the module where the URL patterns are defined.
(b) Discussion
a. What is the purpose of ROOT_URLCONF field in settings.py
 ROOT_URLCONF: The ROOT_URLCONF field in settings.py specifies the Python import path to
the project's URL configuration module. It defines which URL patterns should be used to
resolve incoming requests. By default, it is set to 'myproject.urls', where 'myproject' is the
name of the project.
b. Trace the execution flow with simple line diagram of your created project.
 Execution Flow:
1. When a request is made to the root URL (/), it is received by the Django server.
2. The server checks the ROOT_URLCONF setting to determine the URL configuration module to
use.
3. The URL configuration module (myproject.urls) maps the requested URL to the corresponding
view function.
4. In our case, the root URL (/) is mapped to the hello_world view function defined in views.py.
5. The hello_world view function is called and returns an HTTP response with the content "Hello
World!!".
6. The response is sent back to the server, which delivers it to the client's browser.
7. The browser displays the "Hello World!!" message on the web page.

(c) Output
a. ScreenShot of web page output

(d) Github link


Practical -

4 : Create Django Project to display “ Hello World !! ” in a web page using a Template.
Render the template to display a webpage.

(a) Steps Followed


a. Code Changes in views.py
 In the views.py file, I modified it as follows:

This code imports the render function from django.shortcuts module and modifies the
hello_world view to use a template named "hello_world.html" instead of directly returning a
response.
b. Code changes in urls.py
 In the urls.py file, I modified it as follows:

No changes were required in urls.py compared to Practical-3.


c. Template folder creation and html file changes
 I created a folder named "templates" inside the project directory (at the same level as
manage.py). Inside the "templates" folder, I created a new file named "hello_world.html" with
the following content:
This HTML file serves as a template that will be
rendered by the view and displayed in the web page.
(b) Discussion
a. Compare the difference of implementation between
Practical-3 & 4.
 Practical-3: In Practical-3, the view function
directly returned an HTTP response with the content
"Hello World!!" as plain text. There was no
involvement of templates.
 Practical-4: In Practical-4, the view function uses the render function to render a specified
template. The template file (hello_world.html) contains the HTML structure and content to be
displayed. The rendered template is then returned as an HTTP response.
The use of templates in Practical-4 allows for separating the presentation logic (HTML) from
the view logic (Python code). This separation promotes code organization, reusability, and
easier maintenance.

(c) Output
a. ScreenShot of web page output

(d) Github link

5 : Design a Base Template Class and Child template class with following features to
demonstrate django Template Inheritance support.

Base Class Feature : Display <h1> This is from Base Class


(1) Font size – 14
(2) Font color – Blue

Child Class-1 : Display <h2> content child specific text “ Hello Child-1” in any other color & font.
Practical -

Child Class-2 : Display <h2> content child specific text “ Hello Child-2” in any color and font size.

(a) Steps Followed


a. Code Changes in views.py
 In the views.py file, I modified it as follows:

This code imports the render function from django.shortcuts module and modifies the
hello_world view to use a template named "hello_world.html" instead of directly returning a
response.

(b) Code changes in urls.py


 In the urls.py file, I modified it as follows:

(c) Template folder creation and html file changes for base .html and child1 & 2.html
 I created a folder named "templates" inside the project directory (at the same level as
manage.py). Inside the "templates" folder, I created three files:

• base.html:
• child1.html

• child2.html
The base.html template serves as the base template with common content and styles. The
child1.html and child2.html templates extend the base template and add child-specific content
within the content block.
(b) Discussion
a. What is need of Inheritance support in django template.
 Template inheritance in Django allows you to create a base template with common content
and styles and then extend or override specific sections of the base template in child
templates. It promotes code reuse and helps in organizing and maintaining consistent layout
and structure across multiple pages.
 With template inheritance, you can define a base template with shared elements, such as the
header, footer, navigation menu, and general styling. Then, you can create child templates
that extend the base template and provide specific content, additional styling, or overrides
for certain sections. This approach saves development time, encourages modular design, and
simplifies the maintenance of a consistent user interface.

(c) Output
a. ScreenShot of child 1 & 2 web pages

(d) Github link

Practical -6 : Create following Django Form to accept user data and print same in the VS code
terminal.
(a) Steps Followed
a. Instantiation of Form Class Object
1. Create a Django app or navigate to an existing app in your project.
2. Create a file named forms.py inside the app directory.
3. In forms.py, import the necessary modules:
4. Define a form class that inherits from Django's Form class. Inside this class, define the
fields you want to include in the form:

b. Code Changes in view.py to connect form object with html render file
1. In views.py, import the necessary modules:

c. HTML file changes in template folder


 In search.html, render the form using Django template syntax:
In this HTML template, we render the form fields using {{ form.as_p }} to display them as
paragraphs. The method="POST" attribute in the form tag ensures that the form data is submitted
using the POST method. The {% csrf_token %} template tag adds a CSRF token for security.

(b) Discussion
a. What is django Form Class
 In Django, a Form Class is a Python class that represents a form. It is a fundamental part of
Django's Form handling system, which provides a convenient way to handle HTML form data
and perform validation. Form Classes allow developers to define the fields and validation rules
for a form, and they handle rendering the form in HTML and processing the submitted data.
 Django's Form Class provides various features, such as field types, validation methods,
automatic HTML rendering, handling form submission, and error handling. It abstracts the
complexities of form handling, making it easier to create, validate, and process user input.

b. Explain the fields you have used in generation of the form


 When generating a form using Django's Form Class, you define the fields that the form will
have. Each field represents a form element, such as a text input, checkbox, select box, etc. The
fields you use depend on the data you want to collect from the user. Here are some commonly
used fields and their purposes:
CharField: Represents a single-line text input field for collecting short text inputs.
EmailField: Validates and collects email addresses.
PasswordField: Renders an input field where the user can enter a password. It is used for secure
password inputs.
BooleanField: Represents a checkbox for capturing boolean values (True or False).
ChoiceField: Renders a select box or radio buttons for selecting one choice from a list of
predefined choices.
DateField/DateTimeField: Collects dates or date-time values respectively, providing date pickers or
date-time pickers.
FileField: Allows users to upload files.
In addition to these fields, Django provides various other field types for specific data types and
validation requirements. Each field can have its own attributes, such as label, help text, default
value, validators, and more.
By defining the fields in a Form Class and their associated attributes, Django can handle the
rendering, validation, and processing of form data automatically.

(c) Output
a. ScreenShot of form generated

(d) Github link

Practical -7 : Create following Django Form to accept user data to create sqlite database Table and
update.

( pending next practical )

You might also like