SlideShare a Scribd company logo
Hands On Session on Django
Jul 16, 2016
From
This session
Architecture
Displaying static html pages
Template Language
Django models
ORM
Django
What is Django?
● A Python Framework: designed to support the development of
dynamic websites
Sites Built With Django
Why Django?
● ORM
● Built In Admin Interface
● Internationalization.
● Reusability
● Django takes care of user authentication, content administration, sitemaps,
RSS feeds, and many more tasks
● Testing Libraries.
● Community of 10796 people, 161 countries, 3180 packages and projects.
Architecture.
Browser(Request/Response)
URLS
Views
Templates
Models
Database
Django Installation
$ sudo apt-get install python-setuptools python-dev build-essential
tree
$ sudo apt-get install python-pip python-virtualenv
$ virtualenv <env-name>
$ source <env-name>/bin/activate
$ pip install django
Start New Project!
$ django-admin.py startproject <your-project-name>
$ cd <project-name>
$ tree
$ python manage.py runserver
$ python manage.py startapp <app-name>
$ tree <app-name>
$ python manage.py migrate
Setting.py
● SECRET_KEY
● INSTALLED_APPS
● DATABASES
● TEMPLATES
● STATIC_URL
● AUTH_PASSWORD_VALIDATORS
● TEMPLATE_CONTEXT_PROCESSORS
● MIDDLEWARE_CLASSES
● YOUR_NEW_SETTINGS # You can add new settings
Your First view
=> blog/views.py
from django.http import HttpResponse
def greetings(request):
return HttpResponse("GooD Morning")
=> blog/urls.py
from django.conf.urls import url
from blog.views import greetings
urlpatterns = [
url(r'^greetings/$', greetings, name='greetings'),
]
Rendering html code
blog/views.py
from django.shortcuts import render_to_response
def aboutme(request):
name = "MicroPyramid"
age = 5
return render_to_response('aboutme.html', {
'name': name,
'age': age
})
templates/aboutme.html
<h1>About Me</h1>
<p>my name: {{name}}</p>
<p>age: {{age}}</p>
Models.py
blog/models.py
from django.db import models
from datetime import datetime
class Post(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
createddate = models.DateTimeField(default=datetime.now())
$ python manage.py makemigrations blog
$ python manage.py migrate blog
ORM: Creating data
# Creating new rows with Orm
>>> python manage.py shell
>>> from blog.models import Post
>>> p = Post()
>>> p.title = "learn some math"
>>> p.description = "1+2 = 3"
>>> p.save()
or
>>> Post.objects.create(title="Learn some science", description="E = mc2")
<Post: Post object>
ORM: Reading data
# Reading data from database with ORM
>>> Post.objects.all()
>>> Post.objects.all().count()
>>> for p in Post.objects.all():
... print(p.id)
... print(p.title)
... print(p.description)
ORM: Editing Data
>>> p = Post.objects.get(title="learn some math")
>>> p.title = "Math is Interesting"
>>> p.description = "a2+2ab+b2"
>>> p.save()
>>> p = Post.objects.get(title="Learn some science")
>>> p.delete()
>>> Post.objects.count()
Rendering data from database
blog/views.py
def blog_list(request):
post_list = Post.objects.all()
return render_to_response('postlist.html', {
'post_list': post_list
})
postlist.html
<h1>All Blog Posts</h1>
{% for post in post_list %}
<h3>{{post.title}}</h3>
<p>{{post.description}}</p>
{% endfor %}
Another Example
def blog_detail(request, post_id):
post = Post.objects.get(id=post_id)
return render_to_response('postdetail.html', {
'post': post
})
blog/urls.py
url(r'^blog-detail/(?P<post_id>d+)$', blog_detail, name='blog_list'),
detail.html
<h1>{{post.title}}</h1>
<p>{{post.createddate}}</p>
<p>{{post.description}}</p>
Questions?

More Related Content

PPTX
Custom web application development with Django for startups and Django-CRM intro
PDF
Mini Curso de Django
PPT
Mini Curso Django Ii Congresso Academico Ces
PDF
Drupal Theming
PDF
Introducere in web
PDF
PDF
Web Crawling Modeling with Scrapy Models #TDC2014
PDF
Python, web scraping and content management: Scrapy and Django
Custom web application development with Django for startups and Django-CRM intro
Mini Curso de Django
Mini Curso Django Ii Congresso Academico Ces
Drupal Theming
Introducere in web
Web Crawling Modeling with Scrapy Models #TDC2014
Python, web scraping and content management: Scrapy and Django

What's hot (18)

PDF
G* on GAE/J 挑戦編
PDF
Flask intro - ROSEdu web workshops
PDF
Getting started with php
PDF
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
PPSX
CodeIgniter L4 file upload & image manipulation & language
KEY
Python Development (MongoSF)
PDF
Django Show
PPTX
HackU PHP and Node.js
KEY
Express Presentation
PDF
World of Logging
KEY
What's new in Django 1.2?
PPT
Mongodb
PPTX
Using shortcode in plugin development
PDF
Asynchronous I/O in PHP
PDF
LINE iOS開発で実践しているGit tips
PDF
The report of JavaOne2011 about groovy
PDF
Downloading the internet with Python + Scrapy
PPTX
Boot strap.groovy
G* on GAE/J 挑戦編
Flask intro - ROSEdu web workshops
Getting started with php
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
CodeIgniter L4 file upload & image manipulation & language
Python Development (MongoSF)
Django Show
HackU PHP and Node.js
Express Presentation
World of Logging
What's new in Django 1.2?
Mongodb
Using shortcode in plugin development
Asynchronous I/O in PHP
LINE iOS開発で実践しているGit tips
The report of JavaOne2011 about groovy
Downloading the internet with Python + Scrapy
Boot strap.groovy
Ad

Viewers also liked (20)

PDF
CoderDojo Romagna
KEY
Fuga dalla Comfort Zone
PDF
Filling the flask
PDF
Diabetes and Me: My Journey So Far
PDF
Intro python-object-protocol
KEY
Quattro passi tra le nuvole (e non scordate il paracadute)
PDF
Python Static Analysis Tools
PDF
Online / Offline
PDF
Introduction to SQLAlchemy and Alembic Migrations
PDF
Coderfaire Data Networking for Developers
PPT
Flask - Python microframework
PDF
RESTful Web API and MongoDB go for a pic nic
PDF
Eve - REST API for Humans™
PDF
Introduction to SQLAlchemy ORM
PDF
We Are All Remote Workers
PDF
REST Web API with MongoDB
PDF
Impact of Restful Web Architecture on Performance and Scalability
PDF
Web develop in flask
PDF
Selenium testing
PDF
Django channels
CoderDojo Romagna
Fuga dalla Comfort Zone
Filling the flask
Diabetes and Me: My Journey So Far
Intro python-object-protocol
Quattro passi tra le nuvole (e non scordate il paracadute)
Python Static Analysis Tools
Online / Offline
Introduction to SQLAlchemy and Alembic Migrations
Coderfaire Data Networking for Developers
Flask - Python microframework
RESTful Web API and MongoDB go for a pic nic
Eve - REST API for Humans™
Introduction to SQLAlchemy ORM
We Are All Remote Workers
REST Web API with MongoDB
Impact of Restful Web Architecture on Performance and Scalability
Web develop in flask
Selenium testing
Django channels
Ad

Similar to Hands on django part 1 (20)

PDF
PPTX
Web development with django - Basics Presentation
PPTX
The Django Web Application Framework 2
PPTX
The Django Web Application Framework 2
PPTX
The Django Web Application Framework 2
PPTX
The Django Web Application Framework 2
PPT
DJango
PPTX
Introduction to Django
PPTX
Django Architecture Introduction
PDF
django_introduction20141030
PPTX
Tango with django
PPTX
Basic Python Django
PPTX
Django crush course
PDF
Introduction to Django
PDF
Django Overview
PDF
Introduction to django
PDF
Django Documentation
PDF
Django Workflow and Architecture
PPTX
Why Django for Web Development
Web development with django - Basics Presentation
The Django Web Application Framework 2
The Django Web Application Framework 2
The Django Web Application Framework 2
The Django Web Application Framework 2
DJango
Introduction to Django
Django Architecture Introduction
django_introduction20141030
Tango with django
Basic Python Django
Django crush course
Introduction to Django
Django Overview
Introduction to django
Django Documentation
Django Workflow and Architecture
Why Django for Web Development

More from MicroPyramid . (7)

PPTX
Social login integration
PPTX
Packaging and distributing python code to Pypi
PPTX
Introduction to react_js
PDF
Git & github
PPTX
Django elastic beanstalk
PPTX
Unit Testing with Python
PPTX
Introduction to Vi
Social login integration
Packaging and distributing python code to Pypi
Introduction to react_js
Git & github
Django elastic beanstalk
Unit Testing with Python
Introduction to Vi

Recently uploaded (20)

PPT
Introduction Database Management System for Course Database
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Online Work Permit System for Fast Permit Processing
PDF
System and Network Administraation Chapter 3
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
L1 - Introduction to python Backend.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
System and Network Administration Chapter 2
PPTX
history of c programming in notes for students .pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Introduction to Artificial Intelligence
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Introduction Database Management System for Course Database
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
CHAPTER 2 - PM Management and IT Context
Operating system designcfffgfgggggggvggggggggg
Online Work Permit System for Fast Permit Processing
System and Network Administraation Chapter 3
How to Migrate SBCGlobal Email to Yahoo Easily
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
L1 - Introduction to python Backend.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
System and Network Administration Chapter 2
history of c programming in notes for students .pptx
PTS Company Brochure 2025 (1).pdf.......
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Softaken Excel to vCard Converter Software.pdf
Introduction to Artificial Intelligence
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo Companies in India – Driving Business Transformation.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design

Hands on django part 1

  • 1. Hands On Session on Django Jul 16, 2016 From
  • 2. This session Architecture Displaying static html pages Template Language Django models ORM Django
  • 3. What is Django? ● A Python Framework: designed to support the development of dynamic websites
  • 5. Why Django? ● ORM ● Built In Admin Interface ● Internationalization. ● Reusability ● Django takes care of user authentication, content administration, sitemaps, RSS feeds, and many more tasks ● Testing Libraries. ● Community of 10796 people, 161 countries, 3180 packages and projects.
  • 7. Django Installation $ sudo apt-get install python-setuptools python-dev build-essential tree $ sudo apt-get install python-pip python-virtualenv $ virtualenv <env-name> $ source <env-name>/bin/activate $ pip install django
  • 8. Start New Project! $ django-admin.py startproject <your-project-name> $ cd <project-name> $ tree $ python manage.py runserver $ python manage.py startapp <app-name> $ tree <app-name> $ python manage.py migrate
  • 9. Setting.py ● SECRET_KEY ● INSTALLED_APPS ● DATABASES ● TEMPLATES ● STATIC_URL ● AUTH_PASSWORD_VALIDATORS ● TEMPLATE_CONTEXT_PROCESSORS ● MIDDLEWARE_CLASSES ● YOUR_NEW_SETTINGS # You can add new settings
  • 10. Your First view => blog/views.py from django.http import HttpResponse def greetings(request): return HttpResponse("GooD Morning") => blog/urls.py from django.conf.urls import url from blog.views import greetings urlpatterns = [ url(r'^greetings/$', greetings, name='greetings'), ]
  • 11. Rendering html code blog/views.py from django.shortcuts import render_to_response def aboutme(request): name = "MicroPyramid" age = 5 return render_to_response('aboutme.html', { 'name': name, 'age': age }) templates/aboutme.html <h1>About Me</h1> <p>my name: {{name}}</p> <p>age: {{age}}</p>
  • 12. Models.py blog/models.py from django.db import models from datetime import datetime class Post(models.Model): title = models.CharField(max_length=200) description = models.TextField() createddate = models.DateTimeField(default=datetime.now()) $ python manage.py makemigrations blog $ python manage.py migrate blog
  • 13. ORM: Creating data # Creating new rows with Orm >>> python manage.py shell >>> from blog.models import Post >>> p = Post() >>> p.title = "learn some math" >>> p.description = "1+2 = 3" >>> p.save() or >>> Post.objects.create(title="Learn some science", description="E = mc2") <Post: Post object>
  • 14. ORM: Reading data # Reading data from database with ORM >>> Post.objects.all() >>> Post.objects.all().count() >>> for p in Post.objects.all(): ... print(p.id) ... print(p.title) ... print(p.description)
  • 15. ORM: Editing Data >>> p = Post.objects.get(title="learn some math") >>> p.title = "Math is Interesting" >>> p.description = "a2+2ab+b2" >>> p.save() >>> p = Post.objects.get(title="Learn some science") >>> p.delete() >>> Post.objects.count()
  • 16. Rendering data from database blog/views.py def blog_list(request): post_list = Post.objects.all() return render_to_response('postlist.html', { 'post_list': post_list }) postlist.html <h1>All Blog Posts</h1> {% for post in post_list %} <h3>{{post.title}}</h3> <p>{{post.description}}</p> {% endfor %}
  • 17. Another Example def blog_detail(request, post_id): post = Post.objects.get(id=post_id) return render_to_response('postdetail.html', { 'post': post }) blog/urls.py url(r'^blog-detail/(?P<post_id>d+)$', blog_detail, name='blog_list'), detail.html <h1>{{post.title}}</h1> <p>{{post.createddate}}</p> <p>{{post.description}}</p>