Prince Django
Prince Django
Prince Django
Practical
File
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.
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.
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.
-19 : Install Python including installation of pip, installation and setting up virtual
environment, installation of Django
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:
(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 -
(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.
(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 -
This code defines a view function named hello_world that takes a request object and returns
an HTTP response with the content "Hello World!!".
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.
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
4 : Create Django Project to display “ Hello World !! ” in a web page using a Template.
Render the template to display a webpage.
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) Output
a. ScreenShot of web page output
5 : Design a Base Template Class and Child template class with following features to
demonstrate django Template Inheritance support.
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.
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.
(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
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:
(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.
(c) Output
a. ScreenShot of form generated
Practical -7 : Create following Django Form to accept user data to create sqlite database Table and
update.