Admin
- Congratulations to Sonali, public speaking finalist at Ruhlman!
- Functionality for Hwk5 is graded and entered into Sakai. Remaining 10
points next week.
- Draft versions: comments on 9/17 where I got
a CS304-S14-P3-foo document. Took me about 5 hours for 9
of them. Please submit one ASAP if you want feedback.
- Exam solution distributed (on paper). Graded, I hope, by
Thursday. There are 27, and at 20 minutes each, only 9 hours of grading!
... Starting Friday, I'll give what feedback I can on the Alpha versions.
Definitely by end of reading period.
- Presentations on Thursday. Most people can stay late. I'll make up a
schedule based on that. Invite your friends!
- Thank you, all of you who are here, for your focus and attention even
after it wouldn't matter to your grade. Please don't leave now....
Plan
- Introduce Django.
- Address those questions
- Describe some important infrastructure:
- Let Sarah Xu take over
- Grab bag of other topics, but probably not. Maybe only atpworldtour.com, schedule for Madrid Open, 2015
Django
- it is the entire web server. We saw that last time.
- Django opens its own port, so that it doesn't conflict with Apache
- It runs continuously. Use
^C
in the shell to kill it
- All the code is in a directory that provides configuration info and
software for the same
runserver
command.
- Comes with its own packages for templating and such, though you
replace them with other packages.
Django questions
Address your questions
Ports
With Django, as with node.js, you're not extending an existing HTTP
server, as we are with Apache or Tomcat. You're responsible for
the entire server. You open up a port to do so.
For today, you'll each be running on your own Mac, so there's no
trouble. We can all use port 8000 (the Django default).
Note that this is one reason, among several, that I haven't switched to
Django for this course. I want to figure out a smooth way to integrate a
Django-powered site with Apache-powered Tempest (and other servers).
Django Tutorial Resource
The following is a good
start: Django
Book. It's out of date, but still an excellent introduction to the
ideas and infrastructure, which I think is sadly lacking from other
tutorials.
Background Knowledge: Virtualenv
Separate from Django but crucial in the same way that plumbing is
separate from opening a store.
Virtualenv is a way to allow you, the user, to install a set
of Python packages for a particular project.
- You don't need access to shared, root-controlled directories such
as
/usr/local/lib/python2.7/
.
- Each project can have its own, possibly conflicting, set of Python packages.
- Each project has its own
PYTHONPATH
, determining where
Python loads packages from, when in that project.
- Setting the
PYTHONPATH
and other environment
variables requires source-ing a script in that directory.
This is really cool!
- On your classroom Mac, start a terminal
- Use the
virtualenv
command; it should exist and tell you its usage.
- Do
virtualenv kittens
or some other project name. This
creates a kittens
directory with a bunch of stuff in it for
your kittens
project.
- Do
cd kittens
and ls
. Look around. Look
in bin
.
- Do
printenv VIRTUAL_ENV
and printenv PATH
. Do which python
Do:
python
>>> import sys, pprint
>>> pprint.pprint(sys.path)
>>> quit
- Do
source bin/activate
. (Maybe in a second shell.)
- Repeat the printing of
VIRTUAL_ENV
,
and PATH
and its effect on PYTHONPATH.
Background Knowledge: Pip
Pip is another bit of Python plumbing. It allows you to
- download and install the latest version of a package or module,
- for your version of Python,
- together with any pre-requisite packages and modules (dependencies)
With virtualenv, pip installs to the activated project, rather than to
system-wide directories. Usually, you couldn't use it; now you can!
- In an activated project, do
pip install django
- do an
ls
on the lib/python2.7/site-packages
directory.
- run python, and do
import django
Background Knowledge: SQLite
SQLite is a SQL-based DBMS in a single file. This is nice because you
can easily copy your database from place to place, it's easily locked,
etc. This is the default back-end DBMS for Django.
Turning it Over
I'm now going to turn the class over to Sarah Xu, who took 304 last
year and did an independent study using Django this term.
She wrote this handout for us.
She also provided this zip file for us.
Summary
- Django and frameworks like it are common in web databases.
- The concepts and components are widespread and important:
- URL mappings, factoring code from URL patterns
- URL parsing, using
path/to/view/foo/bar
to
- Templating, particularly with inheritance, which is hard to do
with what we know so far.
- Views as functions in a namespace, as opposed to different
scripts that have to be loaded, parsed, and then run in another
process. Thus, we get a multi-threaded app, like Java Servlets
- Models as a set of objects that map to relational database
tables. That mapping can be automatic. So, you don't have to know
SQL for most of your database interactions.
- Worries:
- You have to worry about writing thread-safe code. The Django
core is thread-safe. That's why the request and response are
arguments, not globals. Whether you write thread-safe views is
another question.
See
Does Django have any tread-safety issues?
- Deployment. Django isn't necessarily the right tool for
retrieving CSS and JavaScript files, so have to play well with
something like Apache.
- Ports: Django is great if it's the only project or app on the
web server, but not so good in an environment like ours.