Getting Started with Django¶
Getting Started Screencast¶
WebFaction has prepared a screencast which will help you get up and running with Django quickly:
You can also download the zip file containing the paste app demonstrated in the video.
Screencast Resources¶
In the screencast, we mentioned some useful resources. Here’s a handy list of hyperlinks from the screencast:
- [0:25] Django
- [1:08] Pastebin
- [8:43] Dive Into Python
- [8:46] Django Documentation
- [8:53] Webfaction Django Forums
Getting Started Tutorial¶
It’s easy to setup a Django application. In the following sections, you will learn how to create a Django application, create the database, setup applications to serve static media, configure Django, and make sure everything is working smoothly.
Create the Django Application¶
First, create a Django application.
Note
If you requested a Django application during sign up, you’ll find an application called django already installed for you. If you see django in ~/webapps or on the Apps page (login required), you can skip this portion of Getting Started with Django.
- Log in to the control panel.
- Click Domains / websites ‣ Applications. The Apps page appears.
- Click the Add new (
) button. The Add page
appears. - In the Name field, enter a name for the application.
- In the App category menu, click to select Django.
- Click the Create button. Your application is created and the confirmation page displayed.
Serving Static Media¶
To reduce memory consumption and improve responsiveness, create a static application to serve static media directly by the server (instead of the Django process).
- Click the Add new (
) button. The Add page
appears. - In the Name field, enter a name for the application.
- In the App category menu, click to select Static.
- In the App type menu, click to select Static only (no .htaccess).
- Click the Create button. Your application is created and the confirmation page displayed.
Creating the Database¶
Next, create a database.
Click Databases > Create new database. The Add page appears.
In the Type menu, click to select PostgreSql.
Enter a name for the database.
Note
The database name must either be your control panel username or begin with your username and an underscore.
Click the Create button. The confirmation page appears.
Make a note of the database password provided; it is required to configure Django to connect to the database.
Creating a Website Entry¶
The next step is to configure the WebFaction control panel to proxy requests to the Django application, static media application, and admin media symbolic link application.
- Click Domains / websites ‣ Websites. The Sites page appears.
- Click the Add new (
) button. The Add
page appears. - In the Name field, enter a name for the website entry.
- In the Subdomains list, click to select the domains to associate with the website record.
- Click the Add new (
) button. A row is added to the
Site apps table. - In the App menu, click to select the Django application.
- In the URL path field, enter /.
- Click the Add new (
) button. A row is added to the
Site apps table. - In the App menu, click to select the static media application.
- In the URL path field, enter /static.
- Click the Create button.
Configuring Your Django Installation¶
Now, configure the Django application to use your new media applications, send email, and connect to your database.
Note
Do you have your own project ready to go or would rather start a project with a different name?
To substitute myproject by creating a new project or uploading your own:
Open an SSH session.
Enter cd ~/webapps/django_application and press Enter.
Enter rm -rf ./myproject and press Enter.
If applicable, upload your project directory to ~/webapps/django_application.
Otherwise, enter ./bin/django-admin.py startproject project_name.
Open ~/webapps/django_application/myproject.wsgi in a text editor.
Change os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' to os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'.
Save your changes and close the file.
Enter mv myproject.wsgi project_name.wsgi and press Enter.
Open ~/webapps/django_application/apache2/conf/httpd.conf in a text editor.
Change WSGIScriptAlias / /home/username/webapps/django_application/myproject.wsgi to WSGIScriptAlias / /home/username/webapps/django_application/project_name.wsgi.
Save your changes and close the file.
Be sure to update your Django settings to use your new database and media applications.
Connecting Django to a Database¶
- Open an SSH session.
- Open /home/username/webapps/django_application/myproject/settings.py with a text editor.
- Add a tuple to ADMINS containing your name and your email address.
- In DATABASES, set ENGINE to django.db.backends.postgresql_psycopg2.
- In DATABASES, set NAME and USER to the name you specified in Creating the Database.
- In DATABASES, set PASSWORD to the database password provided by the control panel.
Configuring Django to Find Static Media and Admin Media¶
While continuing to edit /home/username/webapps/django_application/myproject/settings.py:
Django will automatically include your INSTALLED_APPS‘s static files (for example, the files in someapp/static and the Django admin media), but to include additional directories of static files, set STATICFILES_DIRS to a tuple of directory paths. For example:
STATICFILES_DIRS = ( '/path/to/static/files/', '/path/to/other/static/files/', )
Set STATIC_ROOT to '/home/username/webapps/static_media_app/' where static_media_app is the name of the static media application.
Set STATIC_URL to 'http://domain/static/' where domain is the domain name for your Django site.
Configuring Django to Send Email Messages¶
While continuing to edit /home/username/webapps/django_application/myproject/settings.py:
- At the end of the file, add EMAIL_HOST = 'smtp.webfaction.com'.
- At the end of the file, add EMAIL_HOST_USER = 'mailbox_username'.
- At the end of the file, add EMAIL_HOST_PASSWORD = 'mailbox_password'.
- At the end of the file, add DEFAULT_FROM_EMAIL = 'valid_email_address'.
- At the end of the file, add SERVER_EMAIL = 'valid_email_address'.
Note
The email addresses entered for SERVER_EMAIL and DEFAULT_FROM_EMAIL must be set up in the control panel.
Enabling the Django Admin Site¶
While continuing to edit /home/username/webapps/django_application/myproject/settings.py:
- Add 'django.contrib.admin' and 'django.contrib.admindocs' to INSTALLED_APPS.
- Save and close the file.
- Open /home/username/webapps/django_application/myproject/urls.py with a text editor.
- Uncomment from django.contrib import admin, admin.autodiscover(), (r'^admin/doc/', include('django.contrib.admindocs.urls')), and (r'^admin/', include(admin.site.urls)),.
- Save and close the file.
Synchronizing the Database and Storing Static Files¶
Now, use manage.py to store your static files and synchronize the database.
- Change to the project directory. Enter cd /home/username/webapps/django_application/project and press Enter.
- Enter pythonX.Y manage.py syncdb and press Enter. The database will be updated. You may be prompted to create a superuser; if so, create one now.
- Enter pythonX.Y manage.py collectstatic and press Enter. The static files for your Django apps and the files in STATICFILES_DIRS are copied to your static media application.
Restarting Apache¶
The final stage of the process is to restart Apache.
- Change to Apache’s bin directory. Enter cd /home/username/webapps/django_application/apache2/bin and press Enter.
- Run ./restart.
You can now open http://domain/admin and admire your admin site.
See also
If you run into any problems with your new Django application, see Django Troubleshooting.
