Hands on django part 1
-
Upload
micropyramid- -
Category
Software
-
view
322 -
download
1
Transcript of Hands on django part 1
Hands On Session on Django
Jul 16, 2016From
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.
●It’s open source.
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 codeblog/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?