Lesson 3 - Create a Project
LEARN
Reading - Django for Beginners
- Chapter 2 - Hello World App
Today's Agenda
- Explain how Django apps are structured
- Demonstrate project workflow
- Introduce Views, Routes, Templates
Django Projects
- Outer application layer
- Django App - reusable modules with data and view
Building an App
- URL Routes - which pages are handled
- Views - format the response for the request
- Templates - HTML content in the page
- Data Models (if using a database)
HTML Templates
- Templates can be defined to hold all the HTML content
- The Template Loader must be defined in settings.py
Programming Environment
- Virtual Environment or System Environment
- Install Django
- Application Path
Workflow
- Run Visual Studio
- Pull code to work on
- Build code
- Test code
- Commit & push changes
BUILD
Start a project
Create a new Django project
$ cd BACS350
$ mkdir week1/Hello
$ cd week1/Hello
$ django-admin startproject config .
$ python manage.py migrate
Run the web server
$ python manage.py runserver
Browse to web page
http://localhost:8000
Start an app
Create an app called "pages"
$ python manage.py startapp pages
Add "pages" to INSTALLED_APPS in "config/settings.py"
Create a view
pages/views.py
from django.http import HttpResponse
def homePageView(request):
return HttpResponse('Hello World')
Create a URL route
config/urls.py
from django.urls import path
from pages.views import homePageView
urlpatterns = [
path('', homePageView)
]
Run server
Run the web server
$ python manage.py runserver
Browse to web page
http://localhost:8000
Improve the view
pages/views.py
from django.http import HttpResponse
def homePageView(request):
return HttpResponse('<h1>Hello There</h1><p>This is cool.</p>')
Create a new route
config/urls.py
from django.urls import path
from pages.views import homePageView, testPageView
urlpatterns = [
path('', homePageView)
path('test', testPageView)
]
Improve the view
pages/views.py
from django.http import HttpResponse
def testPageView(request):
return HttpResponse("<h1>This is a test</h1>" + \
"<p>I'm trying to think but nothing happens</p>")
Commit your code
Add source to Git
$ git add .
Commit to repo
$ git commit -m 'Configure Hello app'
Push the repo to Github
$ git push
Project 1 - Part C
- Complete Project 1 C Instructions