Introduction
If you love writing Python and are looking to begin developing web applications, look no further — Django is the choice for you. Django is a popular free and open source web framework that uses Python as its primary development language. It is advertised as the web framework for perfectionists with deadlines. It is particularly suited for complex database-driven projects. Django adheres to Model-View-Controller and highly emphasizes modularization. Stack Harbor’s Django Stack makes it easy to build a fully-functioning web app. The Django Stack consists of virtualenv to manage your dependencies, gunicorn to manage your web server, and nginx to proxy requests to gunicorn & serve static assets.
Renaming the Application
We’ve set up a skeleton application for you named sample_application complete with a virtual environment that contains all the dependencies required for a basic Django application. However, you should rename this project to fit your own needs. We’ve preconfigured your stack with a handy rename script that you should run before you modify any of the Django project files. Run rename_django and follow the instructions to rename the project.
Accessing Your Application
If you point your browser to your public IP address, you will see the default Django page that is shown on every fresh install. In addition, if you point your browser to http://your_ip_address/admin/, you’ll see the Django admin interface which you can use to create users, access your database, and modify permissions. However, you won’t be able to log into this interface until you create an admin account. To do that, we’ll first need to SSH into the remote machine.
If you don’t know how to SSH into your machine, check out our detailed “Getting Started With Your Stack” tutorial. Once you’ve accessed your machine, change directories into /home/django. If you run ls here, you’ll see a directory that houses your Django application. The name of this directory will depend on whether you’ve followed our steps above. Let’s pretend you haven’t yet run the rename_django script and that the directory is still named sample_application. If you cd into this directory, you’ll see two folders named venv and sample_django. venv hosts your virtualenv files and sample_django contains your Django project files. Whenever you need to run commands relevant to your Django project, you have to activate the virtual environment:
source /home/django/sample_application/venv/bin/activate
You should see (venv) to the left of your command line prompt now. This means that your virtual environment is active. Now we want to create an admin user we can use to login to the administrator interface. To do so, we have to use the ‘manage.py’ script in the sample_django folder. This python script allows you to perform many administrative duties from the command line. Assuming you’re still in the /home/django/sample_application folder, run the following commands:
cd sample_django python manage.py createsuperuser
You’ll be prompted to enter a username, an email, and a password. Once you’ve done that, all you have to do is restart the gunicorn process and the nginx server and your changes will take hold.
restart sample_application && service nginx restart
Now if you visit http://your_ip_address/admin/ and use the login information you just set up, you’ll be prompted with the admin interface!
Final Words
Congratulations! You’re all set to begin developing web applications in Python using Django, Gunicorn, and nginx. Check out the excellent documentation written by the folks who take care of the Django project. In addition, check out our Community Section for more tutorials on various tasks related to development and server administration. From all of us at Stack Harbor, ahoy!