Accelerating Information Technology Innovation

Similar documents
Accelerating Information Technology Innovation

South Africa Templates.

Accelerating Information Technology Innovation

정재성

Django: Views, Templates, and Sessions

Accelerating Information Technology Innovation

The Django Web Framework Part II. Hamid Zarrabi-Zadeh Web Programming Fall 2013

widgets, events, layout loosely similar to Swing test browser, or plugin for testing with real browser on local system

Django urls Django Girls Tutorial

Django Part II SPARCS 11 undead. Greatly Inspired by SPARCS 10 hodduc

MIT AITI Python Software Development Lab DJ1:

django-scaffold Documentation

web frameworks design comparison draft - please help me improve it focus on Model-View-Controller frameworks

Quoting Wikipedia, software

Rapid Development with Django and App Engine. Guido van Rossum May 28, 2008

Reminders. Full Django products are due next Thursday! CS370, Günay (Emory) Spring / 6

Lecture 10(-ish) Web [Application] Frameworks

Accelerating Information Technology Innovation

Djam Documentation. Release Participatory Culture Foundation

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

generates scaffolding/framework for models, views

TailorDev Contact Documentation

Theming with Twig in Drupal 8. John Jennings Developer, Johnson & Johnson

Django. Jinja2. Aymeric Augustin DjangoCong 2016

CIS192 Python Programming

webkitpony Documentation

The syndication feed framework

Django PAM Documentation

CSE 115. Introduction to Computer Science I

CIS192 Python Programming

Django starting guide

django-contact-form Documentation

django-xross Documentation

django-facebook-graph Documentation

Webdev: Building Django Apps. Ryan Fox Andrew Glassman MKE Python

alphafilter Documentation

colab Documentation Release 2.0dev Sergio Oliveira

Let s Talk About Templates. Armin

Django Extra Views Documentation

django-session-security Documentation

django-ratelimit-backend Documentation

Web Programming and Design. MPT Senior Cycle Tutor: Tamara Week 2

Functional Programming & Web Frameworks

Introduction to Web Development

Django EL(Endless) Pagination Documentation

Django AdminLTE 2 Documentation

Accelerating Information Technology Innovation

django-revproxy Documentation

django-openid Documentation

Gargoyle Documentation

Moving to a Sustainable Web Development Environment for Library Web Applications

django-report-tools Documentation

Django_template3d Documentation

wagtailmenus Documentation

CS50 Quiz Review. November 13, 2017

Django Extras Documentation

django-conduit Documentation

LA TROBE UNIVERSITY SEMESTER ONE EXAMINATION PERIOD CAMPUS AW BE BU MI SH ALLOWABLE MATERIALS

django-facebook-graph Documentation

django-inplaceedit Documentation

djangotribune Documentation

Understanding this structure is pretty straightforward, but nonetheless crucial to working with HTML, CSS, and JavaScript.

Django-Select2 Documentation. Nirupam Biswas

Django with Python Course Catalog

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

CIS 408 Internet Computing Sunnie Chung

Trees. Carlos Moreno uwaterloo.ca EIT

IN4MATX 133: User Interface Software

Lightweight. Django USING REST, WEBSOCKETS & BACKBONE. Julia Elman & Mark Lavin

Detects Potential Problems. Customizable Data Columns. Support for International Characters

Diving into Big Data Workshop

DJOAuth2 Documentation

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Client Side JavaScript and AJAX

MapEntity Documentation

Software Test Plan Version 1.0

Header. Article. Footer

CSC 309 The Big Picture

Collection Information Menu. Navigation, pages, and related-links quickstart guide

Course Title: Python + Django for Web Application

Welcome to CS50 section! This is Week 10 :(

DDR & jquery More than just hover & dropdown

Microsoft Expression Web Quickstart Guide

wagtailmenus Documentation

CMSC201 Computer Science I for Majors

Flask-Genshi Documentation

Quick Start Guide. This guide will help you get started with Kentico CMS for ASP.NET. It answers these questions:

Programming I. Course 9 Introduction to programming

Themes and Master Pages

Manual Html A Href Javascript Window Open In New

CSS, Cascading Style Sheets

django-ajax-form-mixin Documentation

UNIT 3 SECTION 1 Answer the following questions Q.1: What is an editor? editor editor Q.2: What do you understand by a web browser?

Technology modeling. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Json Parse Error - No Json Object Could Be Decoded Django Rest Framework

wagtailtrans Documentation

Rails: Views and Controllers

welcome to BOILERCAMP HOW TO WEB DEV

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

Django-CSP Documentation

Transcription:

Accelerating Information Technology Innovation http://aiti.mit.edu/program/philippines-summer-2012/ Philippines Summer 2012 Lecture 4 Django Views and Templates June 28, 2012

Agenda Django Views Django Templates 2

Django Views 3

Views What are they (who did the reading??) Views are the logical interface between data (Models) and presentation (Templates)

Hello World #inside views.py (create it) from django.http import HttpResponse def hello(request): return HttpResponse("Hello world ) # EVERY view takes a request object as first parameter # EVERY view returns an HttpResponse object

How to hook it up? #use urls.py from django.conf.urls.defaults import * from mysite.views import hello urlpatterns = patterns('', ('^hello/$', hello), )

Request Life Cycle 1. A request comes in to /hello/. 2. Django determines the root URLconf by looking at the ROOT_URLCONF setting. 3. Django looks at all of the URLpatterns in the URLconf for the first one that matches /hello/. 4. If it finds a match, it calls the associated view function. 5. The view function returns an HttpResponse. 6. Django converts the HttpResponse to the proper HTTP response, which results in a Web page.

Dynamic Content from django.conf.urls.defaults import * from mysite.views import hello, current_datetime, hours_ahead urlpatterns = patterns('', (r'^hello/$', hello), (r'^time/$', current_datetime), )

Dynamic Content from django.http import HttpResponse import datetime def hello(request): return HttpResponse("Hello world") def current_datetime(request): now = datetime.datetime.now() html = "<html><body>it is now %s.</body></html>" % now return HttpResponse(html)

Dynamic URLs from django.conf.urls.defaults import * from mysite.views import hello, current_datetime, hours_ahead urlpatterns = patterns('', (r'^hello/$', hello), (r'^time/$', current_datetime), (r'^time/plus/(\d{1,2})/$', hours_ahead), )

Dynamic URLs from django.http import Http404, HttpResponse import datetime def hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) html = "<html><body>in %s hour(s), it will be %s.</ body></html>" % (offset, dt) return HttpResponse(html)

A Note about Development Where to start, views or URLconfs? Big Picture: Start with URLconfs get an idea of what kind of content you need to deliver to-do list Bottom Up: Start with Views first make the pieces, then put the puzzle together

Tricks with URLconfs Factor out common prefixes Before: urlpatterns = patterns('', (r'^one/$', myapp.views.someview), (r'^two/$', myapp.views.someotherview), (r'^three/$', myapp.views.evenotherview), )

Tricks with URLconfs Factor out common prefixes After: urlpatterns = patterns( myapp.views', (r'^one/$', someview), (r'^two/$', someotherview), (r'^three/$', evenotherview), ) urlpatterns+= patterns( myotherapp.views,.

Extra Parameters to Views # urls.py from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^liststyle1/$', views.list_view, {'template_name':'template1.html'}), (r'^liststyle2/$', views.list_view, {'template_name': 'template2.html'}), )

Extra Parameters to Views # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def list_view(request, template_name): m_list = MyModel.objects.filter(is_new=True) return render_to_response(template_name, {'m_list': m_list})

Extra Parameters to Views # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def list_view(request, template_name): m_list = MyModel.objects.filter(is_new=True) return render_to_response(template_name, {'m_list': m_list}) ^^^ TEMPLATE CONTEXT #this is called

Generic Views Django comes with some commonly used views redirect a user to another page render a specific template display list and detail view of objects display date-based objects in archive pages

Generic Views #Example: direct_to_template from django.conf.urls.defaults import * from django.views.generic.simple import direct_to_template urlpatterns = patterns('', (r'^about/$', direct_to_template, { 'template': 'about.html' }) ) #Magic!!

Loose Coupling Changes made to one piece of code should have little or no effect on other pieces of code to change URL from /hours_ahead to / plus_hours, need to change only URLconf to change View from calculating hours ahead to hours ago, need to change only view Allows linking multiple URLs to the same view

Loose Coupling def hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) html = "<html><body>in %s hour(s), it will be %s.</ body></html>" % (offset, dt) return HttpResponse(html) #HTML should be in a Template!!

Django Templates 22

weather.html <html> <head> <title> Weather </title> </head> <body> <p>today s weather in {{ city }} is {{ description }}.</p> <div id= temperature > {% for day in thisweek %} <li> On {{ day.date }}, the temperature will be {{ day.temperature }}. </li> {% endfor %} </div> <div id="ads"> {% block ads %} Click on these ads! {% endblock %} </div> </body> </html>

Context city = Manila descrip1on = 'sunny thisweek = [dict(date='thursday', temperature=20), dict(date='friday', temperature=25), dict(date='saturday', temperature=22)] Displayed by browser Today s weather in Manila is sunny. On Thursday, the temperature will be 20. On Friday, the temperature will be 25. On Saturday, the temperature will be 22. Click on these ads!

Syntax template.render(context) week = [dict(date='thursday', temperature=20), dict(date='friday', temperature=25), dict(date='saturday', temperature=22)] weather.render({city: Manila, description: sunny, thisweek=week})

Shortcut from Views # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def list_view(request, template_name): m_list = MyModel.objects.filter(is_new=True) return render_to_response(template_name, {'m_list': m_list})

Templates A text-based template for HTML, CSS, XML, JavaScript, etc. Mixture between hard-coded text and abstractions Abstractions Variables Tags Re-useable and extensible

Hard-coded Text in weather.html <html> <head> <title> Weather </title> </head> <body> <p>today s weather in {{ city }} is {{ description }}.</p> <div id= temperature > {% for day in thisweek %} <li> On {{ day.date }}, the temperature will be {{ day.temperature }}. </li> {% endfor %} </div> <div id="ads"> {% block ads %} Click on these ads! {% endblock %} </div> </body> </html>

Variables {{ variable }} If variable doesn t exist, then output TEMPLATE_STRING_IF_INVALID (default: empty string ) {{ variable.attribute }} 1. Dictionary Lookup. variable[ attribute ] 2. Attribute Lookup. variable.attribute 3. Method Call. variable.attribute() 4. List-index Call. variable[attribute]

Variables in weather.html <html> <head> <title> Weather </title> </head> <body> <p>today s weather in {{ city }} is {{ description }}.</p> <div id= temperature {% for day in thisweek %} <li> On {{ day.date }}, the temperature will be {{ day.temperature }}. </li> {% endfor %} </div> <div id="ads"> {% block ads %} Click on these ads! {% endblock %} </div> </body> </html>

Filters Modify the output of variables {{ variable filter }} foo := Hello World bar := [ a, b, c ] {{ foo lower }} --> hello world {{ bar length }} --> 3 {{ bar slice: :2 }} --> [ a, b ] {{ some default: error! }} --> error!

Tags for loops if clauses comments blocks and many more built-in tags (look them up!) {% tag %} {% endtag %}

Tags in weather.html <html> <head> <title> Weather </title> </head> <body> <p>today s weather in {{ city }} is {{ description }}.</p> <div id= temperature {% for day in thisweek %} <li> On {{ day.date }}, the temperature will be {{ day.temperature }}. </li> {% endfor %} </div> <div id="ads"> {% block ads %} Click on these ads! {% endblock %} </div> </body> </html>

For loops {% for x in y %} logic {% endfor %} fruit_basket := { apples, oranges, pineapples } {% for fruit in fruit_basket %} <li>{{ fruit }}</li> {% endfor} <li>apples</li> --> <li>orange</li> <li>pineapples</li>

If clauses {% if <condition> %} logic {% else %} logic {% endif %} {% if rain > 1 } Buy an umbrella for {{ price1 }} {% else %} Buy sunglasses for {{ price2 }} {% endif %}

Comments {% comment %} This comment won t be displayed! {% endcomment} Ignore everything inside tag For inline comments, use {# blah blah blah #}

Template Inheritance Define extensible parts of a template with block tags {% block name %} {% endblock %} Create child templates that can extend blocks Load parent template with {% extends parent_template %}

weather.html <html> <head> <title> Weather </title> </head> <body> <p>today s weather in {{ city }} is {{ description }}.</p> <div id= temperature > {% for day in thisweek %} <li> On {{ day.date }}, the temperature will be {{ day.temperature }}. </li> {% endfor %} </div> <div id="ads"> {% block ads %} Click on these ads! {% endblock %} </div> </body> </html>

ads.html {% extends "weather.html" %} {% block ads %} {% if rain > 1 } Buy an umbrella! {% else %} Buy sunglasses! {% endif %} {% endblock %}

Context city = Manila descrip1on = 'sunny thisweek = [dict(date='thursday',temperature=20), dict(date='friday', temperature=25), dict(date='saturday', temperature=22)] rain = 3 Displayed by browser Today s weather in Manila is sunny. On Thursday, the temperature will be 20. On Friday, the temperature will be 25. On Saturday, the temperature will be 22. Click on these ads! Buy an umbrella!

Template Inheritance In child template, redefine contents of the parent s block tag similar to overriding methods in class inheritance If a block tag is not redefined, then use contents of block tag in parent {{ block.super }} explicitly refers to contents of block tag in parent

ads.html {% extends "weather.html" %}

Questions? 43

Lab 4 Create the views and the template for the mini social networking website FriendBook 44