Setting up Django, Python and MySQL

This config is intended to help reader to quickly setup a Django Web development environment with Python 3 and MySQL on Debian Linux 8 Jessie. The outcome will be installed Django Framework ( 1.7.1 ) and Python ( 3.4.2 ). Once completed and would like to learn more by creating your first Django app you can continue with the official Django Documentation by skipping the first part about settings up the Django environment.

Let's begin by installation of Django Framework and Python 3: The below command will install both Django Web development Framework and Python 3 packages:
# apt-get install python3-django
Next, we would like to change the default python version from Python 2.7 to Python 3.4. For this we will use update-alternatives command.
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
The above commands will install Python alternatives to be used by update-alternatives command. Python3.4 was given a higher priority 2 and therefore, after the execution of the above commands your default python version should change to:
$ python --version
Python 3.4.2
otherwise run:
# update-alternatives --config python
to change your Python version to Python 3.4. Once done you should be able successfully run the following command:
$ python -c "import django; print(django.get_version())"
1.7.1
and therefore be able to create a new Django project eg. mysite:
$ django-admin startproject mysite
Next, we need to install MySQL server and relevant database connector. This can be achieved by the below command:
# apt-get install mysql-server python3-mysql.connector
At this stage we need to create a database eg.mysite and user django with password django-pass to be connected with our previously created Django project mysite. Login as root to your MySQL database:
# mysql -p
and execute all mysql commands shown below:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database mysite;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'django'@'localhost' IDENTIFIED BY 'django-pass';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON mysite.* to django@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Once done locate the mysite/mysite/settings.py configuration file within your new Django project directory and change the following lines from:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
to:
DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.mysql',
        'ENGINE': 'mysql.connector.django',
        'NAME': 'mysite',
        'USER': 'django',
        'PASSWORD': 'django-pass',
        'HOST': 'localhost',
    }
}
Save the settings and create a MySQL tables to confirm the correctness of your configuration. From within your Django project's root directory run:
$ python manage.py migrate
Operations to perform:
  Apply all migrations: auth, sessions, contenttypes, admin
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK
The above command will create the following MySQL tables:
mysql> show tables;
+----------------------------+
| Tables_in_mysite           |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
10 rows in set (0.00 sec)
Lastly, we confirm the completeness of our Django development setup by starting a development server:
$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
February 12, 2015 - 15:56:51
Django version 1.7.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C
setup django develoment on Debian Linux 8 jessie

No comments:

Post a Comment