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

Django-1

Uploaded by

malok9199
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Django-1

Uploaded by

malok9199
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CSE 304

PYTHON PROGRAMMING WITH WEB


FRAMEWORKS
UNIT – 4

Django
Dr.S. Priya/AP-III/ICT

CSE 304 Python programming


CSE 304 Python programming By Dr.S.Priya
• Web frame work
– web application framework
– designed for web applications
– pre desinged models/template structure
– Frame work Vs library
– MVT ( Model - View - Template)
ü Model:
– dynamic data structure, indendent of the user interface , input data -> tables
ü View
– representation of information such as a chart, diagram or table
– browser user interface
– Accepts request and send the response\
– HTML tag (template) CSE 313 Cryptography and Network Security
ü Template
– HTML tag (.html file) Programming in C
17-Apr-23 CSE 304 Python programming By Dr.S.Priya 2
Django

• Django webframe work


– Makes easier to create web pages

– Implemented using python

– Model – View- Template (MVT) pattern

– Django takes care of the difficult stuff so that you can concentrate on building your web applications

– DRY (Don’t Reprat Youerself)

ü develpoed in one place not repeat again & again

CSE 313 Cryptography and Network Security


Programming in C
17-Apr-23 CSE 304 Python programming By Dr.S.Priya 3
• Move on to working directory and verify the python version

• Create virtula Environment==> python -m venv Sample

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 4


Virtual environment

• to keep denmdencies among diffenernt projects

CSE 313 Cryptography and Network Security


Programming in C
17-Apr-23 CSE 304 Python programming By Dr.S.Priya 5
• to verify Django installed or not => pip list

• installing Django => python -m pip install django

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 6


• to verify Django version
– pip list

– python -m django --version

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 7


In VS code...

• After vitrual environment is created in Command (cmd) prompt -> for remaing part move
on to VS code.
• VS Code:
– Select the virtual environment folder.
– change the interpreter path:
ü create one temporary python file like
– new file -> pythonfile -> ‘test.py’
» bottom shows the python version -> click that version -> small pop window will be opened
» select Find option
» open scripts folder -> select the python file -> select interpreter
» some times the virtual environment not get activated
» giveCSE 313 Cryptography
permission and Network Security
in power shell

Programming in C
17-Apr-23 CSE 304 Python programming By Dr.S.Priya 8
InPowerShell

• Select PowerShell and then right click -> Run as administrator -> type the following
commands
• Get-ExecutionPolicy _List
– skows local machine -> undefined (or) restricted
• Set - ExecutionPolicy Unrestricted
– type “y” - > to give permision

CSE 313 Cryptography and Network Security


Programming in C
17-Apr-23 CSE 304 Python programming By Dr.S.Priya 9
• open new terminal in VScode

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 10


• now Acivate the script file in VScode terminal:
– (Sample) PS E:\ACADEMIC\2022-2023\EVEN\Python\Sample> cd scripts
– (Sample) PS E:\ACADEMIC\2022-2023\EVEN\Python\Sample\scripts> Activate.bat

CSE 313 Cryptography and Network Security


Programming in C
17-Apr-23 CSE 304 Python programming By Dr.S.Priya 11
Creating new project in VS code

• Switch over to working directory and activate virtual environment


django-admin startproject <projectname>
django-admin startproject myfirstproj

17-Apr-23 CSE 304 Python programming 12


Creating new project in VS code

• in project folder (myfirstproj)

17-Apr-23 CSE 304 Python programming 13


Run the project

• To run the project


1. select the project folder (myfirstproj)
2. change the interpreter path
– select virtual envt folder (Sample)-> select scripts-> select python file -> give “select
interpreter”
– open a new terminal . now in terminal window path changed to virtual envt (Sample)

3. in termnal windoe type python manage.py runserver command to start the server

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 14


Create a project in cmd prompt

• To create a new project


– activate the virtual environment
– type the following command

django-admin startproject myfirstproj

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 15


Execute the project in webbrowser

4. open any web browser -> in addresss bar type the local host address ( shown in step 3)
http://127.0.0.1:8000/

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 16


New App creation in Django

1. Switch over to working directory and activate virtual environment


2. Create a new project
3. Create a new app
4. add application name in “settings.py” file
5. create an user defined function definition in “views.py” file
6. make appropriate changes in “urls.py” file
7. Run the server
1. start the server

2. run the server in web browser

CSE 313 Cryptography and Network Security


Programming in C
17-Apr-23 CSE 304 Python programming By Dr.S.Priya 17
1. Creating new project in VS code

• Switch over to working directory and activate virtual environment

django-admin startproject <projectname>


django-admin startproject myfirstproj

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 18


Creating new project

• django-admin.py startproject myfirstproj • __init__.py


• Creates a folder like – Empty, Package
– firstproj • settings.py
ü manage.py – Add all user application with existing app
ü myfirstproj • urls.py
– __init__.py
– Project level URL info
– settings.py
– Connect web-apps with project
– urls.py
– Receive user request
– agsi.py
– wsgi.py • agsi.py , wsgi.py
– Web gateway interface

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 19


2. Creating new app in VS code

• change to project directory


python manage.py startapp <appname>
python manage.py startapp firstapp

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 20


2. Creating new app

• Switch to project(firstproject) directory • __init__.py


and type – Package
python manage.py startapp firstapp • admin.py
• sampleapp is created like this – Register the model to Django admin
– firstproject – controller of the apps
ü manage.py – Super user
ü firstproject • apps.py
– __init__.py(package) – Application configuration of the apps


settings.py
urls.py
• models.py
– wsgi.py – Models of web application
– asgi.py – Fields and attributes of the database
ü First app • views.py
– __init__.py(package)
– All the views interacts with client
– admin.py
– apps.py • test.py
– models.py – Contains the code of different test case
– tests.py
– views.py , - migrations(dir), - db.sqlit3

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 21


3. settings.py (in project folder)

• \myfirstproj\myfirstproj

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'firstapp',
]

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 22


4. In views.py(in app folder)

• In path\myfirstproj\firstapp

from django.shortcuts import render


from django.http import HttpResponse

def display(request):
return HttpResponse("<h1> Hello world </h1>")

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 23


In urls.py (in project folder)

• \myfirstproj\myfirstproj

from django.contrib import admin


from django.urls import path
from sampleapp.views import display

urlpatterns = [
path('admin/', admin.site.urls),
path('',display),
]

17-Apr-23 CSE 304 Python programming By Dr.S.Priya 24


Starting Server and Testing

• Starting server (in VS code -> terminal window)


– python manage.py migrate

– python manage.py runserver

• Testing (in web browser)


– Type http://127.0.0.1:8000

CSE 313 Cryptography and Network Security


Programming in C
17-Apr-23 CSE 304 Python programming By Dr.S.Priya 25

You might also like