Skip to content

ecosensors/Django

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND

(The following README and the related Django App, are histories of all steps to make my App running, during my learning time on Django)

The project is still on progress ... :)

Exercises

DocBlog

This is a simple exercise to be familirized with Django (See DocBlog -> src )

EcoSensor

There are two exercises 1) leaflet and 2) Console. leaflet exercise is done following this tutorial, step by step. Then I used this new skills to build the console application with a database which contain a bit less than 1 million of measures. the console exercise is build with some javascript libraries as Leaflet for the map, Chartjs to display the measures. The goal of that exercise is to build a Django app to replace this PHP app (See)

Prepare and install Django

This installation is done on MacOS Monterey (12.6) and then on a MacOS Ventura 13.0.1 I use MAMP 6.6 as local server and PHP 7.4.21, MySQL 5.7.34 and PHPMyAdmin 5.1.0.

Before the next steps, I imported a .sql file to the database 'db_console', with a bit less of 1 millions of measures.

Python3

  • Make sure you have installed Command Line Developper tools
xcode-select --install
  • I personally switched my default shell to bash
chsh -s /bin/bash

and I added the following simple lines to .bash_profile

# nano .bash_profile
export PS1="\[\033[1;31m\]\u:\[\033[0m\]\w$ "
alias ll='ls -laG'
  • Make sure you installed Python3 and pip3
python3 --version
pip3 -V
  • Install brew if it is not done
brew --version
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

virtualenv

Install virtualenv

pip3 install virtualenv

Go to your project folder and create an environment

cd /my-project/folder
python3 -m venv .env        # Create an environement named .env
source .env/bin/activate    # Activate your environment
deactivate                  # Quit your environment

Installation of Django

Preparation

Make sure you have installed mysql-client

brew install mysql-client

You need to edit the .bash_profile file and add the following line

PATH="/usr/local/opt/mysql-client/bin:$PATH"

and then reload your bach_profile

source ~/.bash_profile

Then go to your environment

cd /my-project/folder
source .env/bin/activate   # Activate your environment

and install mysqlclient on your OSX

(.env) pip3 install mysqlclient

Installation

(.env) pip3 install django==4.1.4
(.env) python3 -m django --version

Note, you can have a requierements.txt file with all needed packages

(.env) pip3 install -r requirements.txt # Install packages listed in the requirements file
(.env) pip3 freeze > requirements.txt # Extract the installed package into the requirements.txt file

example of a requierements.txt file

asgiref==3.6.0
sqlparse==0.4.3
django==4.1.4
djangorestframework==3.14.0
djangorestframework-gis==1
django-filter==22.1.0
djangorestframework-simplejwt==5.2.2

You can now create a Django project and app.

To create a Django project

(.env) django-admin startproject console

To create a Django application

(.env) cd console
(.env) ./manage.py startapp map

To configure the database parameters

Go to 'console' folder and the edit settings.py file. Replace the following lines: (Keep in mind, I am using MAMP as an Apache local server)

DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.sqlite3',
        'ENGINE': 'django.contrib.gis.db.backends.mysql',
        #'NAME': BASE_DIR / 'db.sqlite3',
        'NAME': 'db_console',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',

    }
}

Specification

As my application use geospacial coordinate, the command ./manage.py inspectdbwill generate a error:

django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library

This is because djangorestframework-gis and GDAL need to be install on my OSX

Installation of djangorestframework-gis

pip3 install djangorestframework-gis

Installation of GDAL

Info: GDAL GDAL - Linux GDAL - Homebrew

Mac OSX:

brew install gdal

The above command is enough to cancel the GIDAL library error, but you should be interested about pip3 download gdal (see above link)

Check the database and create the models.py file

You can now test the database

source .env/bin/activate   # Activate your environment
(.env) ./manage.py inspectdb

models.py

cd /your/project/location/
(.env) ./manage.py inspectdb > models.py

Keep in mind, I am using an existing database with a lot of measures. The stations are in different fields and I localise them with latitude and longitude. I need to create a 'location' column in my table of my database and I need to adapt/update the models.py file, as well. For that reason, I need to generate new database schema changes based on my models.py file.

First, let’s create the migration files

(.env) ./manage.py makemigrations

Now let’s use that information to update the database

(.env) ./manage.py migrate

Create a superuser (you will be asked to chose an username and a password and an e-mail)

./manage.py createsuperuser

Start your app

(.env) ./manage.py runserver 127.0.0.1:8080

Before using the database cache, you must create the cache table with this command:

./manage.py createcachetable

Help

Uesfull links:

This help to solve some problems

About

Exercises with Django

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages