Django Workshop 2013.06.22
Django Workshop 2013.06.22
website.com/category/
website.com/category/id/article-slug/
website.com/api/category.json
website.com/api/category/id.json
URL Examples
website.com/
website.com/sports/
website.com/sports/123/ufc-champion/
website.com/api/sports.json
website.com/api/sports/123.json
URL Design
Dev Machine
1. Python
http://www.python.org/getit
2. Pip
sudo easy_install pip
3. VirtualEnv
sudo pip install virtualenv
4. Git
http://git-scm.com/downloads
Hands-on Walkthrough
Code of Conduct
(python-env) (pycon-env)
Walkthrough 1
Project
• Renders HTML
• Uses custom pseudo programming syntax
• Very designer friendly
Philosophies
Discourage redundancy.
Be decoupled from HTML.
XML should not be used a template language.
View
Model Tpl
URLConf
• Hooks a particular URL pattern to a view
function
• Uses regex for maximum flexibility
• Table of contents for site or app
urlpatterns = patterns('',
url(r'^articles/(\d{4})/$', 'news.views.year_archive'),
url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)
Philosophies
Loose coupling.
Infinite flexibility.
Definite URLs.
Walkthrough 8
Querying the Model
• Retrieve all objects (aka SELECT *)
all_articles = Article.objects.all()
• Sorting objects
articles = Article.objects.all().order_by(‘title’)
articles = Article.objects.all().order_by(‘-title’)
Article.objects.filter(title__exact=‘Django is my Pony’)
Article.objects.filter(title=‘Django is my Pony’)
Article.objects.filter(title__contains=‘Python’)
Article.objects.filter(title__startswith=‘Django’)
Article.objects.filter(pub_date__gte=‘2013-02-01’)
Article.objects.filter(category__slug__contains=‘global’)
Walkthrough 9
Template Engine
• Renders HTML, CSV, JSON, XML... any text format,
just specify content-type
• Uses basic programming constructs
• Call function or do logic:
{% ... %}
• Print variable:
{{ foo }}
• Filters:
{{ foo|bar }}
Walkthrough 10
Template Engine
• {% load %} - loads custom template tag
Content
Footer
Content
Footer
Walkthrough 11
Recap
• What is Django?
• What is MTV?
• What is Loose Coupling?
• What is KISS?
• What is RAD?
• What is DRY?
Recap
• What is the advantage of using VirtualEnv?
• After installing a new package, what do you do
next?
• What is an app?
• How do you create a Python module?
• After activating new apps in settings, what do
you do next?
Recap
• What is the Admin site?
• What do you do if you want your models to
appear in the Admin site?
• Write a query that gets 5 articles by a certain
author and sort them alphabetically.
• What is the use of dunder methods in queries?
• What is template inheritance?
Maraming Salamat!
Bonus: Deploy to
Heroku
• Heroku is PaaS on top of AWS (IaaS)
• Installs necessary dependencies by reading the
requirements.txt
• In Procfile we declare our manage.py
runserver command
• Easy deployment using git push!
Walkthrough 12