| |||||
Django NotesIndex
Setting up development environmentThe latest version of Django (as of August 2012) is already installed on the server. We have provided an example Django site for the beer drinkers' database. To set it up, run "/home/dbcourse/examples/django-db-beers/setup.sh". This command will set up the directory django.my.net/ in your home directory as well as a new PostgreSQL database called mysite for use by Django. In this process, you will be prompted to create a Django admin user, which can be used to manage the Django site. You can now access the Django site at the URL http://django.my.net/admin/. A Django primerDjango is a Python-based web development framework with following features:
We now explain the structure of the directory ~/django.my.net/:
Common tasksApache-relatedWhile many changes you make to your site will be reflected automatically by reloading the page, the safest thing to do is to restart the Apache web server, using the command "sudo /etc/init.d/apache2 restart". For debugging, you will find the error log in /var/log/apache2/error.log useful. Database-relatedYour Django site uses a database bearing your project name, in this case mysite. Django automatically sets up the database schema based on your models. This step is already done when you set up the site (see /home/dbcourse/examples/django-db-beers/setup.sh if you want the details). However, whenever you change your app data model or add/remove apps, you need to run "./manage.py syncdb" in the ~/django.my.net/ directory to make sure that the database tables are consistent with your apps. Note that Django doesn't support primary keys consisting of multiple columns. For example, in our beer drinkers' database, Django doesn't allow (beer, drinker) to be key of Frequents; instead, it creates a surrogate id for Frequents. For this reason, Django's tables are actually different from those specified in /home/dbcourse/examples/db-beers/create.sql. If you are curious about how Django creates the schema, use "./manage.py sqlall db_beers"; this command will show you the SQL commands (but won't actually run them). You can access the database directly through the PostgreSQL command-line processor (see PostgreSQL Notes). The command is "psql mysite", where mysite is the database name. In most cases, however, you probably just need to use Django's built-in web-based interface for administering the database. You can access that at the URL http://django.my.net/admin/. To log in, use the Django admin user name and password you specified when setting up the site. Django can automatically load data into the database using "fixtures"; see Django documentation for more details. For our example, since we already have the load script for the beer drinkers' database, we use that instead. The data is already loaded when you set up the site, but if you ever need to "reset" the database content (after you modified it, for example), run "(cd /home/dbcourse/examples/db-beers/; psql mysite -af load.sql)". Note that the \COPY commands leave out surrogate ids in their column lists, and the data files do not specify values of these ids either. The database automatically generates them. Adding an appWhen adding a new app app_name, remember to (all paths below are relative to ~/django.my.net/):
As an example, you can try adding a new app called db_beers_alt by following the steps above. This app has pretty much the same functionality as db_beers but uses fewer advanced Django features, and does not use django-social-auth. To avoid clashing with db_beers, it operates on a separate set of tables whose names are prefixed with "db_beers_alt_". To add the db_beers_alt app, start with "cp -r /home/dbcourse/examples/django-db-beers/db_beers_alt/ ~/django.my.net/db_beers_alt/". For settings and URL mapping, follow db_beers as an example. You should set up the app to be accessible via http://django.my.net/db-beers-alt/. After ./manage.py syncdb, to load data, use "psql mysite -af /home/dbcourse/examples/django-db-beers/init-db-beers-alt.sql". Then, collect static files. Changing an app
Coding tipsAvoid hardcoding URLs and paths in your code as much as possible. Instead, use reverse URL mapping and variables PROJ_ROOT and PROJ_NAME (initialized in settings.py). Doing so will make it much less painful when you need to move your site/apps. Django provides of lots handy libraries. For example, Django can automatically generate forms for editing an object and its associated objects. See db_beers views and templates for examples. Of course, you are free to roll your own forms if you want more control; the db_beers_alt app takes this approach. You almost don't need to write SQL in Django; Django's syntax for simple things like key lookups and following foreign keys is convenient. However, for slightly more complex queries, it gets ungainly quickly and can be harder to read and debug than SQL. You can see some examples of SQL vs. Django syntax in db_beers_alt's views. django-social-authdjango-social-auth is a Django package that allows visitor to your site to authenticate using their social network accounts (at Twitter, Facebook, Google, for example). Our example website demonstrates the use of this package. When you visit a page that requires login, you will be redirected to a page where you can choose to login with your Twitter or Facebook account, or an account created by our local Django site (e.g., the admin account; note that we did not provide an interface for new users to register with our site, but we could). For the Twitter and Facebook logins to work, you will need to provide correct settings in ~/django.my.net/mysite/settings_social_auth.py for the following variables: TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, FACEBOOK_APP_ID, and FACEBOOK_API_SECRET. Follow the instructions on how to obtain correct settings for Twitter and for Facebook. Note that logging out from our Django site doesn't imply logging out from the social network accounts. For example, Facebook may keep you logged in for an extended period of time before prompting you again for password. Therefore, when you log into our site again through Facebook, Facebook will consider you as already logged in, and you will be redirected to your target page without being asked for password. Additional information
|
|||||
Last updated Wed Sep 12 21:58:00 EDT 2012 |