Saturday, 25 January 2020

Django

Django provides a framework, written in python, for a webserver.  It appears to be a "professional" product suitable for public web-sites and seems to be well regarded.  I also wanted to see how appropriate it is to use python for web-site implementation and polish my rusty python skills.

Installation


I decided to use the tutorial under WSL (Windows Sub-system for Linux) although a proper implementation would be on my internet-facing RPi. As WSL is not updated automatically with Windows I had to update WSL to a newer release first, then had to install pip and a "lightweight" virtual environment for running python.  Finally I could follow djangoproject website instructions to install Django.  I also installed mysqlclient so that I could access MariaDB on my back-end server but ended up using the django internal sqlite for the tutorial as it was easier.

Tutorial

There is a long and clearly written tutorial to get you started on django.   Django uses the python package directory layout so files are grouped in highly organised structures.  There is a folder for the webserver and a folder for each "app".  Three principle types of python files are used to create a basic app:
  • views - display data in response to a query (URL) submitted to the server
  • models - define the database layout and structure for data stored on the site
  • urls - link the user query(URL) to the correct app and pass parameters (within the URL).
Within the first two parts of the tutorial I was able to create a couple of database fields, generate maintenance pages for those fields and display pages containing them.  This only took an hour or so (I cheated by doing plenty of cut and pasting).  In comparison with PHP/mysql it was much easier to access and maintain data.  

My first app


Most of my website content comprises static pages plus javascript with php used to obtain server information.  Django isn't musch concerned with web pages themselves or client side processing, the server will send out pages but it is only "interested" in python programs, so it replaces the php programming component.

Just from the information in the first tutorial I can easily, create an app and display a web page including javascript.  All I have to do is create an app, create a view and link a url to it.

view

html

result

Without any of the faffing about associated with php I can focus the power of python scripts on the creation of web pages.  In particular it is much easier to incorporate database operations into a python script and code is simpler to understand, write test and read.  

With great power comes great responsiblity.  I will have to check security before I let any web pages out in the wild as python has wide access to local resources.  I also need to check the capabilities of the django server in comparison with lighttpd.  It my be best to use a combination of both, lighttpd for static pages with lots of formatting and django for database, file and communications based work.








No comments:

Post a Comment