Lesson 4 - App Structure
LEARN
Today's Agenda
- Get the tools setup - Python, Venv, Django
- Build an app from scratch
- Test your app with the dev server
Command line
- Windows powershell
- Mac linux
- Command Line
Tools
- Python 3
- python3 --version
- Virtual Environment
- python3 -m venv .venv
- source .venv/bin/activate # On mac
- .venv/Scripts/activate.bat # On windows
- pipenv install
- pipenv shell
- deactivate
- python3 -m venv .venv
- Django
- pip freeze
- pip install django
templates
- define HTML for pages
- template loader to lookup templates
views.py
- logic to convert a request into a response
- written in Python
- calls hidden logic that does a lot of work
urls.py
- listens for specific requests
- define URL routes to pages
- maps the request onto a view
BUILD
Setup workflow
- new terminal window
- activate virtual environment
- edit window
- browser
Create a new project (Lesson 3)
Build the project
$ cd BACS350
$ mkdir week2/Hello
$ cd week2/Hello
$ django-admin startproject config .
$ python manage.py migrate
$ python manage.py runserver
Browse to web page
http://localhost:8000
Commit the changes
$ git add .
$ git commit -m 'Configure Hello app'
$ git push
Create a template
templates/about.html
<h1>My Name is Inigo Montoya</h1>
<p>Prepare to die</p>
templates/about.html
<h1>Home Page</h1>
<p>There's no place like home</p>
Create a route
config/urls.py
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('about', TemplateView.as_view(template_name="about.html")),
path('home', TemplateView.as_view(template_name="home.html")),
]