Introduction To Django

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

Online Internship Programme.

Subject : Web App Development using Python - DJANGO

Installation of DJANGO Framework


1. Install Python

Get the latest version of Python at https://www.python.org/downloads/ or with your


operating system’s package manager.

2. Set environmental Variables

 Click Windows Start Button -> Type Environment Variables - > Click the first
option - > Click Environment variables Button

o OR

 Right Click on This PC => Click Properties menu=> Advanced System Settings
link=> Environment Variables Button => Click on path in System Variables =>
Click Edit Button

 Click New Button => Type the path as


C:\Users\USERNAME\AppData\Local\Programs\Python\Python39

 Again Click New Button => Type the path as


C:\Users\USERNAME\AppData\Local\Programs\Python\Python39\Scripts

3. Install IDE Microsoft Visual Studio Code

The projects can be handled effectively using Microsoft Visual Studio Code, the most
popular IDE.

 Download from https://code.visualstudio.com/download

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO

 Install the Downloaded file with default settings

4. Install DJANGO

 Go to Command Prompt (click start = > run => Type cmd => Click ok)

 Type the Command pip install django

5. Create a Project

 Go to Command Prompt (click start = > run => Type cmd => Click ok)

 Go to a desired project Directory (Create folder if needed)

For Example I want to create a project in the folder d:\ projects

Steps:

> d:

> md projects (to create folder projects)

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO
> cd projects (Change to projects folder)

(OR use Powershell window from your project folder in This PC)

Command to create a project (Example project name first)

> django-admin startproject first

The above command will create a project first in the folder d:\projects with some
basic projects files

> cd first

6. Run the Server

> python manage.py runserver

The above command will start the DJANGO default Development Server at
http://127.0.0.1:8000/

7. To check the output

 Go to any Browser

 Type http://127.0.0.1:8000/ in address bar

You can see the Home page of DJANGO

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO

8. Open Your Project in VS Code.

 Go to Command Prompt (click start = > run => Type cmd => Click ok)

 Navigate to your Project Folder (Eg. d: \projects\first)

 Type code . and click enter

D:\projects\first> code .

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO

 Click the Terminal menu => New Terminal

Create Your First App


It is always better to create an app for each task inside your project

In the Terminal window of VSCode type

> python manage.py startapp firstapp

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO

 Modify the setting.py of your project as mentioned below

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO
 Create a Folder “templates” in the firstapp for all your html pages.

 Create a file inside the templates folder and give any name for it (eg.
home.html)

home.html Content

<h1>Welcome to Futuro Focus</h1>

views.py (in firstapp) content

from django.shortcuts import render

def homepage(request):

return render(request,’home.html’)

Change a line in urls.py of your Project

from firstapp import views #add this code at the beginning

urlpatterns = [

path('',views.homepage), #modify this line

path('admin/', admin.site.urls),]

Run the Server

In the Terminal of VSCode

> python manage.py runserver

The above command will start the DJANGO default Development Server at
http://127.0.0.1:8000/

To check the output

 Go to any Browser

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO
 Type http://127.0.0.1:8000/ in address bar

You can see the output of home.html

Create a Dynamic HTML Page


Usually html Page is a static page, but DJANGO can make a html page as dynamic
page.

Modify a line in the views.py

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO
Modify a line in the home.html

Check the output in browser, You will get the dynamic content

 Create a static folder in your app , in that create a style sheet style.css

h1{

color: crimson

 Change the home.html

Contact: [email protected] website: futurofocus.in


Online Internship Programme.
Subject : Web App Development using Python - DJANGO

<html>

<Head>

<Title>DJANGO</Title>

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">

</Head>

<body>

<h1>Welcome to Futuro Focus - {{name}}</h1>

</body>

</html>

OUTPUT

Contact: [email protected] website: futurofocus.in

You might also like