I am a software development engineer @ Amazon AWS. I graduated from UPenn and Brandeis. I hope everyone stay strong and stay healthy in these challenging times.
Backend Tools
Random NodeJS Notes, JSON handling
To read existing JSON content from local files:
1 | const fs = require('fs') |
To save text to JSON files:
1 | const fs = require('fs') |
Django Basics, Part 1
Django directory levels
- manage.py: we use this to communicate with Django server
- project directory:
- __init__.py: tells Django to use directory as a python package
- settings.py: Django configuration file
- urls.py: url schema
- 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
2import 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
10DATABASES = {
'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
9INSTALLED_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
2python manage.py makemigrations # create migration file
python manage.py migrate # execute migration file
To add new entries with manage.py shell commands
- enter shell:
1
python manage.py shell
- import packages in shell:
1
2
3from myApp.models import Grades, Students
from django.utils import timezone
from datetime import * - add new entry to db:
1
2
3
4
5
6grade1 = Grades()
grade1.gname = "something"
grade1.gdate = datetime(year=,month=,day=)
grade1.ggirlnum = 70
grade1.gboynum = 35
grade1.save() - some other command:
1
2
3
4
5
6Grades.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…)