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

Module 4df

asdfasfasfasfafasdfasdfasfafdsfasdfafafaddfasfafasfasfasfasdfasfasfasfasfasfdasfsdafasdfasf asfasfasfafafsfaf

Uploaded by

narendranvel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module 4df

asdfasfasfasfafasdfasdfasfafdsfasdfafafaddfasfafasfasfasfasdfasfasfasfasfasfdasfsdafasdfasf asfasfasfafafsfaf

Uploaded by

narendranvel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

20CS210 PROGRAMMING

PARADIGMS III – Module 4


Web Frameworks: Django
Introduction
◎ Django is a web development framework that assists in building and
maintaining quality web applications.

◎ Django helps eliminate repetitive tasks making the development


process an easy and time saving experience.

◎ Django is a high-level Python web framework that encourages rapid


development and clean, pragmatic design. Django makes it easier to
build better web apps quickly and with less code.
Django: Design Philosophies
◎ Django comes with the following design philosophies −

◎ Loosely Coupled − Django aims to make each element of its stack independent of the others.

◎ Less Coding − Less code so in turn a quick development.

◎ Don't Repeat Yourself (DRY) − Everything should be developed only in exactly one place instead
of repeating it again and again.

◎ Fast Development − Django's philosophy is to do all it can to facilitate hyper-fast development.

◎ Clean Design − Django strictly maintains a clean design throughout its own code and makes it easy
to follow best web-development practices.
Advantages of Django
◎ Object-Relational Mapping (ORM) Support
○ Django provides a bridge between the data model and the database
engine, and supports a large set of database systems including MySQL,
Oracle, Postgres, etc.
◎ Multilingual Support
○ Django supports multilingual websites through its built-in
internationalization system.
◎ Framework Support
○ Django has built-in support for Ajax, RSS, Caching and various other
frameworks.
◎ Administration GUI
◎ Development Environment
Installing Django
◎ Step 1: Create a directory for your project
◎ D:\1SREC\2021-2022\Odd Sem\Appln Programing in Python\
Programs>mkdir myproject
◎ Step 2: Setting up a virtual environment
◎ py -m venv myproject
◎ Step 3: Activate the virtual environment
◎ D:\1SREC\2021-2022\Odd Sem\Appln Programing in Python\
Programs\myproject\Scripts>activate.bat
◎ Output of Step 3:
◎ (myproject) D:\1SREC\2021-2022\Odd Sem\Appln Programing
in Python\Programs\myproject\Scripts
Installing Django
◎ Step 4: Installing Django
◎ py -m pip install Django
◎ (myproject) D:\1SREC\2021-2022\Odd Sem\Appln Programing
in Python\Programs\myproject>py -m pip install Django
◎ Step 5: Starting the Project
◎ django-admin startproject myproject
◎ Step 6: Starting the Production Server
◎ python manage.py runserver ((myproject) D:\1SREC\2021-2022\Odd Sem\Appln
Programing in) Python\Programs\myproject\myproject>python manage.py runserver)
◎ Step 7: Checking whether server is running
◎ Open the URL http://127.0.0.1:8000 in a Web browser
Home Page
Creating a project in Django
◎ To create the first app, go to the directory where the
manage.py file is and executes the following command:
◎ django-admin startapp boards
◎ Configure the created app in settings.py - INSTALLED_APPS
variable
◎ Write the first view in views.py
◎ To tell Django when to serve this view, and is done inside the
urls.py file.
Modifications to be done
◎ Modifications in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'boards',
]
Modifications to be done
◎ Modifications in views.py
from django.http import HttpResponse
def home(request):
return HttpResponse('Hello, World!')
Modifications to be done

◎ Modifications in urls.py
from django.conf.urls import url from django.contrib import admin
from django.contrib import admin from django.urls import path
from django.urls import include, re_path
from boards import views from boards import views
urlpatterns = [
url(r'^$', views.home,
urlpatterns = [
name='home'),
re_path(r'^$', views.home, name='home'),
url(r'^admin/', admin.site.urls), re_path(r'^admin/', admin.site.urls),]
]
Output
Creating a model to add Database Service
◎ Make the following changes in models.py file
class BlogPost(models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
timestamp = models.DateTimeField()
Setting up the Database
◎ Step 1: Install mysql-connector
◎ Step 2: Install mysqlclient
◎ Step 3: Make the following changes in settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'sample',
'USER': 'root',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
Setting up the Database
◎ Step 4: Save changes (Migrations) by running the following
command
◎ python manage.py makemigrations
◎ python manage.py migrate

◎ Step 5: Check for the changes in MySQL.


Setting up the admin
◎ In the urls.py file, make the following changes
◎ admin.autodiscover()
◎ Create a super user by running the following command
◎ python manage.py createsuperuser
Setting up the admin
◎ Make the following Changes in the admin.py file available in
boards directory

from boards import models

class BlogPostAdmin(admin.ModelAdmin):
list_display=('title','timestamp')
admin.site.register(models.BlogPost, BlogPostAdmin)
The admin login screen
Admin home page
Blog Post
Adding a new blog
Output of Blog Creation

You might also like