You are here

30 January 2016
If you plan to use Python for your web backend then you might want to consider Django as there are many good libraries that you need for web development.

Installing Django

Django is used in your project as any other python module. The easiest way would be to get it from your operating system repository or pip.
Source code viewer
  1. # From your operating systems repositories.
  2. yaourt -S python-django
  3.  
  4. # From Python's package manager.
  5. pip install django
Programming Language: Bash
Check if and what version of django you have installed.
Source code viewer
  1. python -c 'import django; print(django.get_version())'
Programming Language: Bash

Creating a project

Go to your project directory and run django-admin command on your command line to create the skeleton for your web development project.
Source code viewer
  1. django-admin startproject mysite
Programming Language: Bash

Apache

  • Make sure you have enabled mod_wsgi, you can see "Running Python on Apache" for more information.
  • I have "/var/www/python" for my python instance. In that folder I have manage.py from the last step.
  • I added "127.0.0.1 python" to my "/etc/hosts". Then my instance opens from http://python/
  • I have configuration for my python instance in vhosts.
Source code viewer
  1. <VirtualHost python:80>
  2. ServerAdmin info@browse-tutorials.com
  3. ServerName python
  4. ServerAlias python
  5.  
  6. WSGIDaemonProcess mysite python-path=/var/www/python:/usr/lib/python3.5/site-packages
  7. WSGIProcessGroup mysite
  8. WSGIScriptAlias / /var/www/python/mysite/wsgi.py
  9.  
  10. <Directory /var/www/python>
  11. <Files wsgi.py>
  12. Order allow,deny
  13. Allow from all
  14. </Files>
  15. </Directory>
  16.  
  17. ErrorLog /var/log/httpd/.python-error_log
  18. CustomLog /var/log/httpd/.python-access-log combined
  19. </VirtualHost>
Programming Language: Text
When this was done and Apache restarted. I had error 500. From the apache log I found out that database is requirement for Django.

MySQL

In "/var/www/python/mysite/" I have "settings.py". The directory "mysite" was created by django-admin. In that file is DATABASES variable, that contains the information.
Source code viewer
  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.mysql',
  4. 'NAME': 'python',
  5. 'HOST': '127.0.0.1',
  6. 'PORT': '3306',
  7. 'USER': 'root',
  8. 'PASSWORD': '',
  9. }}
Programming Language: Python

  • So you could connect to database. "pip install mysql-python"
  • Run "python manage.py migrate" to create neccessary database changes.
  • Everything should be up and running now. There should be a welcome page.