rs
play

rs rts r - PowerPoint PPT Presentation

ts strt q rs rts r t rs r


  1. ▲❡t✬s st❛rt ❜② q✉✐③③✐♥❣ ②♦✉✳ ❘❡♠✐♥❞❡rs ❋✉❧❧ ❉❥❛♥❣♦ ♣r♦❞✉❝ts ❛r❡ ❞✉❡ ♥❡①t ❚❤✉rs❞❛②✦ ❈❙✸✼✵✱ ●ü♥❛② ✭❊♠♦r②✮ ❙♣r✐♥❣ ✷✵✶✺ ✶ ✴ ✻

  2. ❘❡♠✐♥❞❡rs ❋✉❧❧ ❉❥❛♥❣♦ ♣r♦❞✉❝ts ❛r❡ ❞✉❡ ♥❡①t ❚❤✉rs❞❛②✦ ▲❡t✬s st❛rt ❜② q✉✐③③✐♥❣ ②♦✉✳ ❈❙✸✼✵✱ ●ü♥❛② ✭❊♠♦r②✮ ❙♣r✐♥❣ ✷✵✶✺ ✶ ✴ ✻

  3. ❊①✐t q✉✐③✿ P❡rs✐st❡♥t ❉❛t❛ ❛♥❞ ❉❛t❛❜❛s❡s ✐♥ ❲❡❜ ❉❡✈❡❧♦♣♠❡♥t ❚♦ ♠❛✐♥t❛✐♥ ✉s❡r s❡ss✐♦♥s✱ ✇❤❛t ♣✐❡❝❡s ♦❢ ♣❡rs✐st❡♥t ✐♥❢♦r♠❛t✐♦♥ ♠✉st ❜❡ st♦r❡❞ ♦♥ t❤❡ ❝❧✐❡♥t ❛♥❞ t❤❡ s❡r✈❡r s✐❞❡❄ ❲❤❛t ✐s ❙◗▲ ✐♥❥❡❝t✐♦♥ ❛tt❛❝❦ ❛♥❞ ❤♦✇ ❞♦ ②♦✉ ♣r♦t❡❝t ❛❣❛✐♥st ✐t ✐♥ ②♦✉r ✇❡❜❛♣♣❄ ❊♥tr② s✉r✈❡②✿ ❉❥❛♥❣♦ ▼♦❞❡❧✲❱✐❡✇✲❈♦♥tr♦❧❧❡r ✭♦r ❚❡♠♣❧❛t❡✮ ❆r❝❤✐t❡❝t✉r❡ ❲❤❛t✬s t❤❡ ♣♦✐♥t ♦❢ ❉❥❛♥❣♦✬s ✉r❧s✳♣② ✜❧❡❄ ❲❤❡r❡✬s t❤❡ ❞❛t❛❜❛s❡ ✐♥ ❛ ❉❥❛♥❣♦ ❛♣♣ ❛♥❞ ❤♦✇ ❞♦ ②♦ ♠❛♥✐♣✉❧❛t❡ ✐t❄

  4. ❲❡❜ Pr♦❣r❛♠♠✐♥❣ ✐♥ ❉❥❛♥❣♦ ■♥tr♦❞✉❝t✐♦♥ t♦ ✐ts ▼♦❞❡❧✲❱✐❡✇✲❈♦♥tr♦❧❧❡r ✭♦r ❚❡♠♣❧❛t❡✮ ❆r❝❤✐t❡❝t✉r❡ ❈❙✸✼✵ ❙❊ Pr❛❝t✐❝❡ ✫ ❙t❛rt✉♣✱ ❈❡♥❣✐③ ●ü♥❛② ✭❙♦♠❡ s❧✐❞❡s ❝♦✉rt❡s② ♦❢ ❊✉❣❡♥❡ ❆❣✐❝❤st❡✐♥ ❛♥❞ t❤❡ ■♥t❡r♥❡ts✮ ❙♥♦✇♣❛❝♦❧②♣s❡ ❆❚▲ ✷✵✶✹ ❈❙✸✼✵✱ ●ü♥❛② ✭❊♠♦r②✮ ❉❥❛♥❣♦ ▼❱❈ ✐♥tr♦ ❙♣r✐♥❣ ✷✵✶✺ ✸ ✴ ✻

  5. • high-level Python Web framework that encourages rapid development and clean, pragmatic design. • Django was designed to handle two challenges: – intensive deadlines – stringent requirements of the experienced Web developers who wrote it. • It lets you build high-performing , elegant Web applications quickly . • Focuses on automating as much as possible 1/24/2013 CS 370, Spring 2012 18

  6. Approach: Model-View-Controller Formalized in 1979 • Model: maintain state of application. Typically done with a database • View: output to client, usually as HTML code, rendered by a web browser. • Controller: interaction with user. handles user input, processes data and communicates with Model to save state. 1/29/2013 CS 370, Spring 2012 2

  7. MVC 1/29/2013 CS 370, Spring 2012 3

  8. ✶ ❱✐s✐t♦r✬s ❜r♦✇s❡r ❛s❦s ❢♦r ❛ ❯❘▲✳ ✷ ❉❥❛♥❣♦ ♠❛t❝❤❡s t❤❡ r❡q✉❡st ❛❣❛✐♥st ✐ts ✉r❧s✳♣② ✜❧❡s✳ ✸ ■❢ ❛ ♠❛t❝❤ ✐s ❢♦✉♥❞✱ ❉❥❛♥❣♦ ♠♦✈❡s ♦♥ t♦ t❤❡ ✈✐❡✇ t❤❛t✬s ❛ss♦❝✐❛t❡❞ ✇✐t❤ t❤❡ ❯❘▲✳ ❱✐❡✇s ❛r❡ ❣❡♥❡r❛❧❧② ❢♦✉♥❞ ✐♥s✐❞❡ ❡❛❝❤ ❛♣♣ ✐♥ t❤❡ ✈✐❡✇s✳♣② ✜❧❡✳ ✹ ❚❤❡ ✈✐❡✇ ❣❡♥❡r❛❧❧② ❤❛♥❞❧❡s ❛❧❧ t❤❡ ❞❛t❛❜❛s❡ ♠❛♥✐♣✉❧❛t✐♦♥✳ ■t ❣r❛❜s ❞❛t❛ ❛♥❞ ♣❛ss❡s ✐t ♦♥✳ ✺ ❆ t❡♠♣❧❛t❡ ✭s♣❡❝✐✜❡❞ ✐♥ t❤❡ ✈✐❡✇✮ t❤❡♥ ❞✐s♣❧❛②s t❤❛t ❞❛t❛✳

  9. Site design in Django: review • Goal: make common Web-development tasks fast • Step 1: Design your model: model.py (M) – Automatically create DB, APIs, admin interface • Step 2: Design your views: views.py (V) • Step 3: Design application logic: urls.py ( C ) • Step 4: Design your templates: • Step 5: Enjoy free bells & whistles (caching, syndication, …) 1/29/2013 CS 370, Spring 2012 5

  10. Who cares? • Spotify uses Django for some of their web sites. • Instagram uses Django for their web application [10] • Yandex uses Django for their weather forecast site, OpenID provider, blog hosting service, internal Wiki and internal Mail Archives. • Transifex – an open source platform for localization. • Bucketlist – uses Django to let users store life goals. • The Public Broadcasting Service • The Berkeley Graduate School of Journalism • Universal Subtitles uses Django for its free and open source collaborative subtitling site. • FreeNAS uses Django for its web interface in its 8.0 major release. • PBS's Merlin, a platform for managing member stations' video metadata • Google internal stuff (details unknown). • …. 1/24/2013 CS 370, Spring 2012 20

  11. Example Django Project • Tutorial: https://docs.djangoproject.com/en/dev/intro/tutorial01/ – https://docs.djangoproject.com/en/dev/intro/tutorial03/ 1/29/2013 CS 370, Spring 2012 6

  12. Django Models • A model is the single , definitive source of data about your data . • Each model is a Python class that subclasses django.db.models.Model. • Each attribute of the model represents a database field . • Django gives you an automatically-generated database-access API 1/29/2013 CS 370, Spring 2012 7

  13. Example: Person (first_name, last_name) • Define Person: from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) • What happens automatically CREATE TABLE myapp_person ( "id" serial NOT NULL PRIMARY KEY, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL ); 1/29/2013 CS 370, Spring 2012 8

  14. Example: Person (first_name, last_name): 2 • Each field should be an instance of the Field class – The database column type (e.g. INTEGER, VARCHAR) – The widget to use in Django's admin interface, if you care to use it (e.g. <input type="text">, <select>). – The minimal validation requirements, used in Django's automatically-generated forms. • Field reference: https://docs.djangoproject.com/en/1.3/ref/models/fields/#model-field-types • More details: https://docs.djangoproject.com/en/1.3/topics/db/models/ 1/29/2013 CS 370, Spring 2012 9

  15. Django Models • https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs 1/29/2013 CS 370, Spring 2012 10

  16. Recap: Django project pieces • django-admin.py startproject blog – blog/ __init__.py: A file required for Python to treat the mysite directory as a package manage.py : A command-line utility that lets you interact with this project. Try: python manage.py help settings.py : Settings/configuration for this project. urls.py : The URLs for this project (“table of contents”) 1/29/2013 CS 370, Spring 2012 12

  17. Django Project vs. App 1/29/2013 CS 370, Spring 2012 13

  18. Django architecture encourages this 1/29/2013 CS 370, Spring 2012 14

  19. Example: 1/29/2013 CS 370, Spring 2012 15

  20. URL dispatcher • A clean, elegant URL scheme is an important detail in a high-quality Web application. • Django lets you design URLs however you want – There’s no . php or .cgi required • Cool URIs don't change: – http://www.w3.org/Provider/Style/URI 1/29/2013 CS 370, Spring 2012 16

  21. How Django processes a request 1. Determine the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, can be changed. 2. Load URLconf and looks for variable urlpatterns . This should be a Python list, in the format returned by the function django.conf.urls.patterns(). 3. Iterate through each URL pattern, in order , and stop at the first one that matches the requested URL. (REGEX) 4. If one of the regexes matches, import and call the given view , which is a Python function . The view gets passed an HttpRequest as its first argument and any values captured in the regex as remaining arguments . 5. If no regex matches, or if an exception is raised during any point in this process, invoke an appropriate error-handling view . 1/29/2013 CS 370, Spring 2012 17

  22. 2-minute REGEX review http://www.djangoproject.com/r/python/re-module/ Symbol Matches . (dot) Any single character \d Any single digit [A-Z] Any character between A and Z (uppercase) [a-z] Any character between a and z (lowercase) [A-Za-z] Any character between a and z (case-insensitive) + One or more of the previous expression (e.g., \d+ matches one or more digits) [^/]+ One or more characters until (and not including) a forward slash ? Zero or one of the previous expression (e.g., \d? matches zero or one digits) Zero or more of the previous expression (e.g., \d* matches zero, one or more * than one digit) Between one and three (inclusive) of the previous expression (e.g., \d{1,3} {1,3} matches one, two or three digits) 1/29/2013 CS 370, Spring 2012 18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend