Running Django with PostgreSQLIn this tutorial, we will show you how to configure a Django project on your hosting account that uses PostgreSQL as the database backend using the WebApps platform. If you do not see a WebApps section in the Control Panel of your hosting account, this tutorial may not be suitable for your particular hosting environment. Please contact our support team for more information. You need SSH access to the server in order to make the changes described in this tutorial. If you haven't set up SSH for your account yet, you should do this now. Note that you should also enable both "Network tools" and "Compiling tools" on the SSH Access page of the Control Panel of your hosting account. Quick SetupIf you want to quickly set up a Django project, you should first set up a PostgreSQL instance. We would recommend that you use the shell script from the Quick Setup section at the beginning of the article. It will configure PostgreSQL and it will set up the environment of your hosting account for installing Django. After PostgreSQL is running in your account, you can download this Django setup shell script to your account and run it. The script will set up the WebApps project for your Django site. Note that the Django setup script will automatically configure the PostgreSQL login details for your Django application, so you don't have to do this manually. You can simply run the two scripts in sequence:
In that case, you can skip to the "Running the Django Website" section of this article. InstallationBefore you start the installation, you have to make sure that you have a running instance of PostgreSQL in your account and that the After that, you can create the directory structure of your new Django site:
Then, set up the database details:
virtualenvFor Python applications like Django, it is common to create an isolated environment for the application. You can do this with
This way, all Python dependencies will be used only by your site. Python DependenciesYou can now continue to installing the software dependencies of your Django site.
Django ProjectAt this point, you can set up the Django project and configure it:
Creating the WebApps ProjectHere, you have to create the WebApps project. This is necessary because it will determine the port on which your Django application will listen for requests.
WSGI ServerGunicorn is a robust and performant WSGI server meant for production use. The easiest way to start the WSGI server is to use a wrapper shell script:
Running the Django WebsiteWebApps ProjectYou now have to complete the configuration of the WebApps project in order to set up the URL where the application will be accessible. The WebApps project is already created, so you have to click on the "Edit" icon in order to modify its settings: You should set up the domain and subdomain fields: If you want to put your Django application on a separate subdomain, you can create one on the Subdomains page of the Control Panel. Finally, you have to enable the application to run it: Management ScriptAlthough it is not required, it is convenient to create a simple wrapper script to make managing the Django application from the command line easier. This script will take care of setting up the environment for you so that you don't have to do it every time you want to use the
With this wrapper script, you can log in over SSH and directly run any command in your Django project. For example, assuming that you named the project
Logging InThe installation script creates a wrapper shell script that allows you to manage your Django project directly on the command line, without having to first activate the virtualenv environment separately. For example, you can create an administrative username for your Django project like this (assuming the project is named
After you fill out the prompts, you will be able to log in as the administrator of your new Django website. |