Tuesday 28 January 2020

Django CSS,JS,images and databases

I mentioned in the previous post that django isn't very interested in client side processing.  Our static web page code will look very much as before.  We need to be able to include other files, images, javascript, css etc within this code.

I created a test application (called jonas) to check this out.  Its job is simply to display text, formatted by CSS and display a button, which when clicked, calls a javascript function to display a message.
I did find it easier to read the tutorial in pdf format so I referred to django manual

We put the html in a file jonas/templates/jonas/index.html
We put css and js in jonas/static/jonas/style.css and jonas/static/jonas/mess.js
We put a background image in jonas/static/jonas/images/mybgc.jpg
We create a view in jonas/views.py to display jonas/index.html.
You have to add your app name into the INSTALLED_APPS setting and Django then searches for index.html, style.css and mess.js in the appropriate places and displays the appropriate page including css and js.

jonas/views.py
jonas/templates/jonas/index.html
jonas/static/jonas/mess.js
jonas/static/jonas/style.css 
Here is the result:

To make the pages visible from other local devices I used runserver 0:8000 and added the setting ALLOWED HOSTS['*'].  I could now access django from other devices on my network to my WSL IP address.

Databases

Using MariaDB


For the tutorial I used the sqlite database which comes in the django package.  I use MariaDB on an RPI for my databases and some care was required to allow me to use them.  
Firstly I needed to point django at my database server:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'tutorial',
        'USER': 'djangouser',
        'PASSWORD': '######',
        'HOST': '192.168.0.##',
        'PORT': '3306',
    },

We have to create the tutorial database ourselves on the database server. I used a digitalocean tutorial to help me. Briefly you:
signon to mysql on RPI
create a database called tutorial
create a user djangouser@192.168.0.#
grant all privileges to the user

To check that the database was working I could use python in my django environment:
  from MySQLdb import _mysql
  db=_mysql.connect(host=”192.168.0.24”, user=”djangouser”, passwd=”secret”, db=”tutorial”)

It is now possible to use migrate as shown in tutorial part 2.  After doing this I could continue with the tutorial updating the RPI tutorial database.

Using existing databases

To use my existing databases on RPI I looked at django documentation for "legacy" databases.
First  I made my picture database the default in settings.py.  I then ran inspectdb to create models.py with the appropriate database fields.  Checking models.py I saw the primary key field was missing so added it.  I now ran migrate which sets up the tables django requires for database access.  I tested database access using the django shell to ensure I could see data.

Using Multiple databases


I expect my apps to use different databases.  There are a number of ways to set this up.  The simplest is to add .using('dbname') in your database access code.  If you omit it django will access the default database.





No comments:

Post a Comment