UNC BACS 350

Web Apps with Python/Django

Logo

Lesson 3 - Create a Project

LEARN

Reading - Django for Beginners

Today's Agenda

Django Projects

Building an App

HTML Templates

Programming Environment

Workflow

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

Commit all of your code and push