Python Django - JackieWSC/Onepiece GitHub Wiki

Django 2.1

ToDo

  • show multi form on same page and able to update each
    • [done - 2h] add the field in form to let view identify which form is updated

2.1 setup

Create Project and Application

  • Create project "monitorapi"
    • django-admin startproject monitorapi
  • Create application "stocksapi" under the project
    • python manage.py startapp stocksapi
  • Create "templates" folder
    • mkdir templates
  • Create "static" folder
    • mkdir static
  • Sync with database
    • python manage.py makemigrations
    • python manage.py migrate

Env setup

  • Add the application "stocksapi" to settings.py
# fiie:monitorapi/monitorapi/settings.py
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'stocksapi', # new added application
]
  • setup the directory of TEMPLATES
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')], # add templates path
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
  • setup the static folder path
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] # added static path

urls.py setup

urlpatterns = [
    path('', views.index),
    path('index', views.index),
    path('threeday/<int:district>/', views.threeday),
    path('admin/', admin.site.urls),
]

views.py setup

def threeday(request, district=None):
    global df
    columns = ['Date Time',
               'Weekday',
               'Weather',
               'Templature',
               'Temp 2',
               'wind',
               'direction',
               'humitly',
               'rainning',
               'conform']

    df = pandas.DataFrame(columns=columns)
    createDF(district)
    json = df.to_json(orient='records', force_ascii=False)
    return HttpResponse(json)

ForeignKey

restaurant = models.ForeignKey(Restaurant, on_delete=models.PROTECT)

Command used to check and create database

>> python manage.py check
System check identified no issues (0 silenced).

>> python manage.py makemigrations xxx # xxx is apps name
Migrations for 'verity':
  verity/migrations/0001_initial.py
    - Create model CaseStatus
    - Create model RefereesDetails
    - Create model RefereesMain
    - Add field main to refereesdetails
    - Add field details to casestatus

>> python manage.py migrate xxx 0001
Operations to perform:
  Apply all migrations: verity
Running migrations:
  Applying verity.0001_initial... OK

SQL management

python manage.py sqlmigrate myapp 0001_initial

Admin Dashboard

# setup the super user to login the admin page
python manage.py createsuperuser <user_name>

# setup password for super user to login the admin page
python manage.py changepassword <user_name>

# update the admin.py
from django.contrib import admin

# Register your models here.
from stocksapi.models import Visitor

class VisitorAdmin(admin.ModelAdmin):
    list_display = ('date_time','latency')

heroku Setup

  • create a working dir with virtual
virtualenv herokuenv
cd herokuenv
source bin/activate
  • install Django and other package
pip install django
pip install dj-database-url dj-static gunicorn psycopg2
pip install beautifulsoup4 pandas requests
  • heroku base on defined package in requirements.txt to setup the environment
cd monitorapi_heroku
pip freeze > requirements.txt
  • heroku require Procfile to indicate the method to run the website, create Procfile inside monitorapi_heroku folder
web: gunicorn --pythonpath monitorapi monitorapi.wsgi
  • heroku base on the defined python version in runtime.txt to setup the environment, create runtime.txt inside monitorapi_heroku folder
python-3.6.5
  • It betters to create another setting file for heroku, as the environment on heroku is different with local development machine, create prod_settings.py in monitorapi folder which contains settings.py
from .settings import *
STATIC_ROOT = 'staticfiles'
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['monitorapi.herokuapp.com','*']
DEBUG = False
  • Create .gitignore file to save the space on heroku, we don't want to upload files like *.pyc to heroku
*.pyc
__pycache__
staticfiles
  • Update the wsgi.py for heroku
import os

from django.core.wsgi import get_wsgi_application
from dj_static import Cling
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'monitorapi.settings')

application = Cling(get_wsgi_application())

Upload to heroku

heroku login
git init
heroku git:remote -a monitorapi
heroku config:set DJANGO_SETTINGS_MODULE= monitorapi.prod_settings
heroku config:set DISABLE_COLLECTSTATIC=1
git add .
git commit -m "init commit"
git push heroku master

Heroku Postgres

  • SQL database service
  • Hobby Dev is free to use
  • Install PostgreSQL 10 (postgresapp) on mac, there are 3 database in default
    • postgres
    • user
    • template1
  • create the database for app
psql -U postgres
\list
CREATE DATABASE testdb;
DROP DATABASE testdb;
\ connect testdb;

  • Install pgadmin tools (pgAdmin 4) to manage database via UI
  • Local setup
# setup the url link
export DATABASE_URL=postgres://$(whoami)

# pull the database form heroku to local
heroku pg:pull DATABASE_URL herokulocaldb --app monitorapi

# push the database form local to herku
heroku pg:push herokulocaldb DATABASE_URL --app monitorapi

# example:
 ▸    Remote database is not empty. Please create a new database or use heroku pg:reset
Scitys-iMac:monitorapi_heroku Scity$ heroku pg:reset DATABASE
 ›   Warning: heroku update available from 7.21.0 to 7.27.1
 ▸    WARNING: Destructive action
 ▸    postgresql-rugged-43376 will lose all of its data
 ▸    
 ▸    To proceed, type monitorapi or re-run this command with --confirm monitorapi

> monitorapi

How to update the model on heroku

  • Updated the modeles.py, e.g. updated fields or add new table
  • use python manage.py makemigrations to generate the file (stocksapi/migrations/0001_initial.py) under migrations
  • Check in the file
  • git add stocksapi/migrations/0001_initial.py
  • git commit -m Added "new migration file"
  • git push heroku master
  • Let heroku create the table remotely on remote Heroku Postgres
  • heroku run python manage.py migrate

Postgres Setting in Django

  • to connect to a postgreSQL server with python, it needs to install the psycopg2 library:
pip install psyconpg2

# update the database setting in settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'testdb',
        'USER': 'Scity',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432'
    }
}

# create all of the djangop tables (around 10 tables) on new database
python manage.py migrate 

Fixed the admin page broken issue on Heroku

> source bin/activate
> pip install dj-static
> cd monitorapi_heroku
> pip freeze > requirements.txt

Edit the settings.py files:
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

Edit the wsgi.py
from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

Reference

⚠️ **GitHub.com Fallback** ⚠️