Lesson 9 - Database
LEARN
Week 1 - Setup Development Tools
- Lesson 1 - Setup Django
- Lesson 2 - Django Projects & Apps
- Lesson 3 - Create a Project
- Project 1 - Install Python3, Virtual Env, Django
- Django for Beginners - Chapter 1
Week 2 - Build a Django Project
- Lesson 4 - App StructureDisplay HTML Templates
- Lesson 5 - Template View
- Lesson 6 - Static Server
- Project 2 - Build a simple app with TemplateView
- Django for Beginners - Chapter 2
Week 3 - Web App Hosting
- Lesson 7 - Setup Python Anywhere
- Lesson 8 - Deploy Application
- Project 3 - Setup Python Anywhere web hosting
- Django for Beginners - Chapter 3
Week 4 - Data
- Lesson 9 - Database
- Lesson 10 - Admin Views
- Lesson 11 - ListView
- Project 4 - Basic Views
- Django for Beginners - Chapter 4
Data Models
- ORM - Object Relational Mapping
- Data Models define the table structure
- Database tables are created by code
Hero Data Model
hero/models.py
from django.db import models
class Hero (models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
image = models.CharField(max_length=200)
Migration
Examine code for changes to Data Models
$ python manage.py makemigrations
Apply changes to Data Tables
$ python manage.py migrate
BUILD - Development Workflow
Short-cuts
- Sensei
- Github repo
- Web app
- Python Anywhere config
- Python Anywhere console
Development Workflow
- Pull code
- Make changes
- Test changes
- Push changes
- Deploy
Pull code
Before you start working
$ git pull
Make changes
- Work in small steps
- One feature at a time
- Test and fix
- Commit frequently (10-20 minutes)
Test changes
- Change code and refresh browser
- Editor, Terminal, Browser all visible
Commit changes
Commit and push each hour
$ git add .
$ git commit -m 'Week 4 - admin views'
$ git push
BUILD - New Project
Create Project
$ cd BACS350; mkdir week4/Superhero && cd week4/Superhero
$ django-admin startproject config .
$ python manage.py startapp hero
$ python manage.py runserver
Settings
config/settings.py
# Enable the templates for the 'templates' directory
TEMPLATES = [
{
...
'DIRS': ['templates'],
...
},
]
# Enable the static media server (Images, CSS, Javascript)
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
# Enable Python Anywhere
ALLOWED_HOSTS = ['markseaman.pythonanywhere.com', '127.0.0.1']
# Enable data for Hero app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hero',
]
Templates
templates/index.html
<h1>Superhero Gallery</h1>
<a href="/admin/">Django Admin Views</a>
Views
hero/views.py
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = 'index.html'
URLs
config/urls.py
from django.urls import path
from hero.views import IndexView
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('', IndexView.as_view()),
]
Test and fix
Test the app
$ python manage.py runserver
$ browse to 127.0.0.1:8000
Create a super user
$ python manage.py createsuperuser
Django Tests
hero/tests.py
from django.test import SimpleTestCase
class SimpleTests(SimpleTestCase):
def test_home_page_status_code(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
Run all tests
$ python manage.py test
BUILD - Application Deployment
Python Anywhere Setup
- Two Alternatives
- App Environment created by Web App wizard
- Use mkvirtualenv to control environment
- Delete the existing app
Create Virtual Environment
- Create an isolated python environment
- mkvirtualenv --python=/usr/bin/python3.8 .venv
- Install Django in the environment
- pip install django
Delete Your Web App
- Use the Python Anywhere Web App page
- Delete Web App
- Add New Web App "temp"
- Switch settings to "week4/Superhero"
Switching Web Apps
- Code
- Source Code - /home/markseaman/BACS350/week4/Superhero
- Working Directory - /home/markseaman/BACS350/week4/Superhero
- WSGI
- project_home = '/home/markseaman/BACS350/week4/Superhero'
- os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings'
- Static files - /home/markseaman/BACS350/week4/Superhero/static