django

Django Basics, Part 1

Django directory levels

  1. manage.py: we use this to communicate with Django server
  2. project directory:
    1. __init__.py: tells Django to use directory as a python package
    2. settings.py: Django configuration file
    3. urls.py: url schema
    4. wsgi.py: project and wsgi integration web server

Database (MySQL) config

  • dependencies used:
    mysql 5.7.24 h56848d4_0
    mysql-connector-c 6.1.11 hccea1a4_0
    mysql-connector-python 8.0.17 py27h3febbb0_0 anaconda
    mysql-python 1.2.5 py27h1de35cc_0 anaconda
    pymysql 0.9.3 py27_0
  • __init__.py:, add the following code:
    1
    2
    import pymysql
    pymysql.install_as_MySQLdb()
  • in settings.py: add database configurations to connect to the database
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': # server name
    'USER': # user name
    'PASSWORD': # password
    'HOST': 'localhost',
    'PORT': '3306',
    }
    }

Init new application

  • init new application:
    1
    python manage.py startapp <myApp>
  • codes above creates:
    • admin.py: website config
    • models.py: model
    • views.py: view
  • In setting, add to INSTALLED_APP:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myApp'
    ]

models.py file

  • the following is an example of models.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    from django.db import models

    # Create your models here.
    class Grades(models.Model):
    gname = models.CharField(max_length=20)
    gdate = models.DateTimeField()
    ggirlnum = models.IntegerField()
    gboynum = models.IntegerField()
    isDelete = models.BooleanField(default=False)

    def __str__(self):
    return "%s-%d-%d"%(self.gname, self.gboynum, self.ggrilnum)


    class Students(models.Model):
    sname = models.CharField(max_length=20)
    sgender = models.BooleanField(default=True)
    sage = models.IntegerField()
    scontend = models.CharField(max_length=20)
    isDelete = models.BooleanField(default=False)
    sgrade = models.ForeignKey("Grades")

    def __str__(self):
    return "%s-%d-%s"%(self.sname, self.sage, self.scontend)
  • make migration and execute migration file: will create database according to models.py
    1
    2
    python manage.py makemigrations # create migration file
    python manage.py migrate # execute migration file

To add new entries with manage.py shell commands

  1. enter shell:
    1
    python manage.py shell
  2. import packages in shell:
    1
    2
    3
    from myApp.models import Grades, Students
    from django.utils import timezone
    from datetime import *
  3. add new entry to db:
    1
    2
    3
    4
    5
    6
    grade1 = Grades()
    grade1.gname = "something"
    grade1.gdate = datetime(year=,month=,day=)
    grade1.ggirlnum = 70
    grade1.gboynum = 35
    grade1.save()
  4. some other command:
    1
    2
    3
    4
    5
    6
    Grades.objects.all()    # get all data entries in Grades
    g = Grades.objects.get(pk=1) # get the 1st object in Grades
    g.gboynum = 45
    g.save() # alternate existing data
    d.delete() # delete, including the one in db
    stu1 = g.students_set.create(sname=...) # add new student with one line

To run server

  • execute:
    1
    python manage.py runserver ip:port
  • Admin:
    • publish content
    • add/modify/delete content
    • add “‘django.contrib.admin’” in INSTALLED_APPS
    • exists by defualt
    • add “/admin” and log in
    • change language or timezone based on preference

(to be continued…)