I resisted frameworks for a long, long time. Being a do-it-yourself'er by nature - I just hate giving up that fine-grained knowledge of everything that is going on. Time's it seems are changing and I decided to take the plunge. Being an all-or-nothing kind of guy I've been developing now with a mix of Django, Prototype (as opposed to jQuery which I wish would just die a terrible terrible death), and MaterializeCSS. I have to say -- I am VERY impressed with how the structure these approaches bring make spaghetti code readable and bring functionality way easier than if you wanted to roll your own from scratch

This entire website has been constructed on the basis of these 3 frameworks.

For this article I will write about Django and the basics of getting it up and running. Django has a little bit of a learning curve but you will appreciate its structure and power once you get rolling. The framework ships with its own "light development server" but if you're like me you'd just prefer to do your development on something that looks exactly like your deployment environment why would you not. In my case this is Apache.

My environment includes:

  1. Fedora 25
  2. Python 3.5.2
  3. MariaDB 10.1.21
  4. Apache 2.4.25 (Fedora)

Briefly:

CODE:
    
su
dnf install python3
dnf install httpd
systemctl enable httpd.service
systemctl start httpd.service
dnf install mysql-server mysql
systemctl start mariadb
systemctl enable mariadb
    

Lets start from the beginning - I'm not going to go into great detail here about how Django works (Their site has some reasonably ok documentation Right now we are concentrating on just getting everything up and running so you can start coding.

Next step - install pip3 if you don't have it already. important differentiator here pip is for python 2.7, pip3 is for python 3+ Make sure you're using pip3 commands not pip commands if you have both versions of python installed on your system (I did by default on fedora).

CODE:
    
sudo dnf install python3-pip

Fedora may complain that you already have this installed - but the pip3 command is not available. 
to fix you need to run:

dnf reinstall python3-pip \ I had to do this.
    

Then install Django framework via pip3:

CODE:
    
pip3 install django
    


I reference you now to the django docs on creating a project. Fundamentally you need to run django-admin to create the skeleton of a new project.

CODE:
    
django-admin startproject mysite

which generates:
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    



Configuring Apache

Good work! Now you have all the elements installed -- but they still need to be enabled and connected together. I host multiple websites on the same physical hardware - as such I (and I suspect a number of you) will need to configure a VirtualHost for your domain. When you enable the Django framework you need to do more than simply point the VirtualHost to your webserver DocumentRoot.

There are 2 concepts here:

  • Apache needs to understand you want to serve actively generated content from WSGIPython sources
  • Apache also needs to understand that you have some 'static' directories that are not served by the framework and need to be served up just like a classic DocumentRoot
  • Why is this? This is because Django does not like to serve media files. It also needs to rely on certain static files (like css styling etc). So we need to enable these two separate capabilities. Its not really that hard.

    First thing's first. You need to create the static directories you'll point Apache to. Locate them at the top-level of your site (under mysite)

    CODE:
        
    cd /var/www/html/mysite
    mkdir static
    mkdir media
    sudo chgrp -R apache static
    sudo chgrp -R apache media
        

    Have a look at this apache VirtualHost configuration (snippet from httpd.conf which is located under /etc/httpd/conf on Fedora):

    CODE:
        
    WSGIPythonPath /var/www/html/yoursite
    #1VirtualHost *:80#2
    	ServerName www.yourwebsite.com
    	WSGIScriptAlias / /var/www/html/yoursite/yoursite/wsgi.py
    
    	Alias /static/ /var/www/html/yoursite/static/
    	Alias /media/ /var/www/html/yoursite/media/
    
    	#1Directory /var/www/html/yoursite/static#2
    		Require all granted
    	#1/Directory#2
    
    	#1Directory /var/www/html/yoursite/media#2
    		Require all granted
    	#1/Directory#2
    
    	#1Directory /var/www/html/yoursite/yoursite#2
    		#1Files wsgi.py#2
    			Require all granted
    		#1/Files#2
    	#1/Directory#2
    #1/VirtualHost#2
        

    What's going on here?

    First -- you need to declare your WSGIPythonPath Outside your virtual host definition. Then you see the standard VirtualHost declaration that starts the VirtualHost configuration in Apache. The WSGIScriptAlias points to the location of your wsgi.py file that was created automatically by Django when you created your project.

    Then there are two aliases. These set up the "static" region of your site which will not be served by Django but rather directly served by Apache. These are in order: the location of your CSS, fonts, js (static) and media files (media).

    Below this you need to grant Apache permissions to the static and media files (using the new nomenclature which is "require all granted" --- the old version of this is "allow from all").

    finally you give permissions to the Django portion of the site.

    Making the Connection between Django using Python3 and MariaDB

    The default database back-end for Django is SQLlight. Personally i'm a MySql / MariaDB guy so I wanted to substitute this. You do this by editing the root mysite/settings.py file. change the DATABASES section to:

    CODE:
        
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'yourDB',
            'USER': 'YourUsername',
            'PASSWORD': 'yourPW',
            'HOST': 'localhost',
            'PORT':'',
        }
    }
        

    Now comes another issue -- We wanted to use Django because of Python3 support -- but Python3 and MariaDB do not play well together with the current release of Django without a little help. You will get errors complaining that Django is not able to talk with your mysql (maria) backend. To fix this you need to:

    CODE:
        
    git clone https://github.com/clelland/MySQL-for-Python3
        

    Follow the README to install.

    This is the connector that will allow Python3 to talk with MySQL. Good Times. (let me tell you how long it took me to figure THAT out).

    So, at long last, now you have a new project generated, Apache Configured, Django installed and MariaDB connected to it all using Python3

    I hope I have helped someone. Please feel free to offer corrections as I did this all by memory.

    Cheers

    Tommy

    Navigate

    I resisted frameworks for a long, long time. Being a do-it-yourself'er by nature - I just hate giving up that fine-grained knowledge of everything that is going on. Time's it seems are changing and I decided to take the plunge. Being an all-or-nothing kind of guy I've been developing now with a mix of Django, Prototype (as opposed to jQuery which I wish would just die a terrible terrible death), and MaterializeCSS. I have to say -- I am VERY impressed with how the structure these approaches bring make spaghetti code readable and bring functionality way easier than if you wanted to roll your own from scratch

    This entire website has been constructed on the basis of these 3 frameworks.

    For this article I will write about Django and the basics of getting it up and running. Django has a little bit of a learning curve but you will appreciate its structure and power once you get rolling. The framework ships with its own "light development server" but if you're like me you'd just prefer to do your development on something that looks exactly like your deployment environment why would you not. In my case this is Apache.

    My environment includes:

    1. Fedora 25
    2. Python 3.5.2
    3. MariaDB 10.1.21
    4. Apache 2.4.25 (Fedora)

    Briefly:

    CODE:
        
    su
    dnf install python3
    dnf install httpd
    systemctl enable httpd.service
    systemctl start httpd.service
    dnf install mysql-server mysql
    systemctl start mariadb
    systemctl enable mariadb
        

    Lets start from the beginning - I'm not going to go into great detail here about how Django works (Their site has some reasonably ok documentation Right now we are concentrating on just getting everything up and running so you can start coding.

    Next step - install pip3 if you don't have it already. important differentiator here pip is for python 2.7, pip3 is for python 3+ Make sure you're using pip3 commands not pip commands if you have both versions of python installed on your system (I did by default on fedora).

    CODE:
        
    sudo dnf install python3-pip
    
    Fedora may complain that you already have this installed - but the pip3 command is not available. 
    to fix you need to run:
    
    dnf reinstall python3-pip \ I had to do this.
        

    Then install Django framework via pip3:

    CODE:
        
    pip3 install django
        


    I reference you now to the django docs on creating a project. Fundamentally you need to run django-admin to create the skeleton of a new project.

    CODE:
        
    django-admin startproject mysite
    
    which generates:
    mysite/
        manage.py
        mysite/
            __init__.py
            settings.py
            urls.py
            wsgi.py
        



    Configuring Apache

    Good work! Now you have all the elements installed -- but they still need to be enabled and connected together. I host multiple websites on the same physical hardware - as such I (and I suspect a number of you) will need to configure a VirtualHost for your domain. When you enable the Django framework you need to do more than simply point the VirtualHost to your webserver DocumentRoot.

    There are 2 concepts here:

  • Apache needs to understand you want to serve actively generated content from WSGIPython sources
  • Apache also needs to understand that you have some 'static' directories that are not served by the framework and need to be served up just like a classic DocumentRoot
  • Why is this? This is because Django does not like to serve media files. It also needs to rely on certain static files (like css styling etc). So we need to enable these two separate capabilities. Its not really that hard.

    First thing's first. You need to create the static directories you'll point Apache to. Locate them at the top-level of your site (under mysite)

    CODE:
        
    cd /var/www/html/mysite
    mkdir static
    mkdir media
    sudo chgrp -R apache static
    sudo chgrp -R apache media
        

    Have a look at this apache VirtualHost configuration (snippet from httpd.conf which is located under /etc/httpd/conf on Fedora):

    CODE:
        
    WSGIPythonPath /var/www/html/yoursite
    #1VirtualHost *:80#2
    	ServerName www.yourwebsite.com
    	WSGIScriptAlias / /var/www/html/yoursite/yoursite/wsgi.py
    
    	Alias /static/ /var/www/html/yoursite/static/
    	Alias /media/ /var/www/html/yoursite/media/
    
    	#1Directory /var/www/html/yoursite/static#2
    		Require all granted
    	#1/Directory#2
    
    	#1Directory /var/www/html/yoursite/media#2
    		Require all granted
    	#1/Directory#2
    
    	#1Directory /var/www/html/yoursite/yoursite#2
    		#1Files wsgi.py#2
    			Require all granted
    		#1/Files#2
    	#1/Directory#2
    #1/VirtualHost#2
        

    What's going on here?

    First -- you need to declare your WSGIPythonPath Outside your virtual host definition. Then you see the standard VirtualHost declaration that starts the VirtualHost configuration in Apache. The WSGIScriptAlias points to the location of your wsgi.py file that was created automatically by Django when you created your project.

    Then there are two aliases. These set up the "static" region of your site which will not be served by Django but rather directly served by Apache. These are in order: the location of your CSS, fonts, js (static) and media files (media).

    Below this you need to grant Apache permissions to the static and media files (using the new nomenclature which is "require all granted" --- the old version of this is "allow from all").

    finally you give permissions to the Django portion of the site.

    Making the Connection between Django using Python3 and MariaDB

    The default database back-end for Django is SQLlight. Personally i'm a MySql / MariaDB guy so I wanted to substitute this. You do this by editing the root mysite/settings.py file. change the DATABASES section to:

    CODE:
        
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'yourDB',
            'USER': 'YourUsername',
            'PASSWORD': 'yourPW',
            'HOST': 'localhost',
            'PORT':'',
        }
    }
        

    Now comes another issue -- We wanted to use Django because of Python3 support -- but Python3 and MariaDB do not play well together with the current release of Django without a little help. You will get errors complaining that Django is not able to talk with your mysql (maria) backend. To fix this you need to:

    CODE:
        
    git clone https://github.com/clelland/MySQL-for-Python3
        

    Follow the README to install.

    This is the connector that will allow Python3 to talk with MySQL. Good Times. (let me tell you how long it took me to figure THAT out).

    So, at long last, now you have a new project generated, Apache Configured, Django installed and MariaDB connected to it all using Python3

    I hope I have helped someone. Please feel free to offer corrections as I did this all by memory.

    Cheers

    Tommy