browser making a request for the resource at a URL • URL defines the location • Web apps pay attention to path and query • Fragment isn’t even seen by web app (it is used in the browser only) • Web app code usually lives at path • Query (in both GET and POST) are processed by framework • Response from web app is an HTTP response plus HTTP result code • 200 - success • 404 - does not exist • 301/302 - moved foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment
and generating responses • Usually have server built in • Often include some form of data storage library • ORMs are common modeling and querying tools • Provide utility libraries for operating in a web-based environment • URL encoding/decoding • Handle form processing in a safe and standard way • Usually provide recommended structure for organizing business logic • Available in every language • We will be learning Django • Most common and popular python framework • disqus, pinterest, instagram, mozilla, theonion.com, rdio 5
queries • View • Renders response into HTML • Controller • Handles request/response • Processes information, takes action, handles form data • Very powerful methodology that separates out logic and presentation • Used for desktop applications (Mac OS X, Windows .NET) 1 • Used for mobile applications (iOS, Android, Windows Phone) 6 1. https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html Model View Controller Request Update Query Response
of information • Server IP address • Server Port • Client IP address • Client Port • You can have multiple software servers running on one hardware server by binding to a different port • Special ports are less than 1024, which require root priviledges • There are 65535 ports available on a server • Standard HTTP port: 80 • Standard HTTPS (SSL) port: 443 • Standard SSH port: 22 7
views are actually “controllers” • Django templates are actually “views” • Django models are still models • Applications are python modules that live in project directory • Each application has MVC components • Models are in models.py • Views are in views.py • Templates are in templates directory • All static assets live in static directory 8 Django Model Django Template Django View Request Update Query Response
• cd project • Has a default layout • Run server using runserver command • Logs will be output to the terminal • Database is located at sqlite.db (but we’re not using until next time) • The default application is also called project 9 drwxr-xr-x 6 ubuntu ubuntu 4096 Oct 22 03:31 ./ drwxr-xr-x 7 ubuntu ubuntu 4096 Oct 21 15:41 ../ -rwxr-xr-x 1 ubuntu ubuntu 250 Sep 15 18:47 manage.py* drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 15 18:47 media/ drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 16 13:56 project/ -rw-r--r-- 1 ubuntu ubuntu 35840 Sep 16 13:57 sqlite.db drwxr-xr-x 5 ubuntu ubuntu 4096 Oct 22 03:30 static/ drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 22 03:30 templates/
• They only work on paths, not GET or POST query parameters • They are regular expressions • Match multiple paths to a single view • Capture text in a part of the URL (path) and can pass the captured variables into the specified view • They call the specified view • Read more: • https://docs.djangoproject.com/en/1.4/topics/http/urls/#example • http://www.diveintopython.net/regular_expressions/ 11 url(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive')
variables that are translated at render time into data • Have basic logic that executes at render time • Variables are passed in by the view using a dict called the context • Context keys can be used to access data in the template by name • Templates can inherit from one another, overriding blocks in the parent templates • https://docs.djangoproject.com/en/1.4/ref/templates/builtins/ 13 <p>var1: {{ var1 }}</p> <p>var2: {{ var2 }}</p> <p>list_var: <ol> {% for i in list_var %} <li>{{ i }}</li> {% endfor %} </ol>
off my variables!{% endblock %} {% block body %} <p>Index with variables</p> <p>var1: {{ var1 }}</p> <p>var2: {{ var2 }}</p> <p>list_var: <ol> {% for i in list_var %} {{ i }} {% endfor %} </ol> {% endblock body %}
app on the server, type runserver • To view your django app in your browser, go to: • http://dev.hatcherydevshop.com:<port>/ • Find <port> by typing echo $PORT
to templates • Add project to Github • Homework: • Read http://devhub.fm/http-requestresponse-basics/ • Create a view that renders a weekly calendar • HTML table shows 1 week • Pass the current day and the next 6 days into the template and use them to render the days of the week with the correct current date 19