How to get started with Django on top of Python 3.4 + MySQL (MariaDB) 5.5
Introduction
Django (https://www.djangoproject.com/) is an amazing and simple Python MVC framework. Here is how you get started with newer version of MySQL and Python.
Vagrant (optional)
Eventually start out with a fresh VM (Ubuntu + Python3 environment) with this Vagrant box:
https://github.com/adionditsak/vagrant-python-env
Basics
Create a new Python 3.4 isolated environment with Virtualenv (https://virtualenv.pypa.io/en/latest/):
$ virtualenv-3.4 djangoenv
Activate the new Python environment (djangoenv):
$ source djangoenv/bin/activate
Install django lib with pip:
$ pip install django
Install mariadb version 5.5:
$ apt-get install mariadb-server-5.5
Test if django is installed properly:
$ python -c "import django; print(django.get_version())"
Check if Python and MySQL versions are correct:
$ mysql -V
mysql Ver 15.1 Distrib 5.5.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
$ python -V
Python 3.4.0
Start your new django project:
$ django-admin startproject mysite
MySQL Engine
Run runserver or migrate command:
$ python manage.py migrate
$ python manage.py runserver 8080
You will see the following error:
...
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
...
If you read the documentation, you will see that the native MySQLdb driver doesn't support Python 3. The trick is to use the mysql.connector instead. Read more in the documentation: https://docs.djangoproject.com/en/1.7/ref/databases/#mysql-db-api-drivers
Change the ENGINE to mysql.connector.django in your project settings(.py):
$ vi mysite/mysite/settings.py:
....
77 DATABASES = {
78 'default': {
79 'NAME': 'djangotest',
80 'ENGINE': 'mysql.connector.django',
81 'HOSTNAME': 'localhost',
82 'USER': 'root',
83 'PASSWORD': 'testtest123!',
84 'OPTIONS': {
85 'autocommit': True,
86 },
87 }
88 }
...
Now try again to create neccessary db stuff for the default APPS in Django:
$ python manage.py migrate
August 14, 2015 - 08:12:20
Django version 1.8.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
^C(djangovirtualenv)root@vagrant-ubuntu-trusty-64:~/mysite# python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: auth, sessions, admin, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying sessions.0001_initial... OK
(djangovirtualenv)root@vagrant-ubuntu-trusty-64:~/mysite#
Ready to code
Now it should work. You should be ready to start coding your django apps:
$ python manage.py startapp appname