Pro Series - Web Framework Django/python workshop Let's Code Blacksburg! Tuesday, December 13, 2016 from 5:30 PM to 8:00 PM (EST) Blacksburg, VA
-
- Overall Goal of this session:
- Demonstrate the value Django can bring inside your organization
- By showing how fast and easy it is to spin up a DB driven website
- By showing off the autogenerated admin console
- Why Django might be better than a typical CMS (WordPress, Drupal, Joomla, etc)
- Basically all CMS's will need a programmer to extend and customize and you are still left with a messy customization
- Django start with a developer and get exactly what you need.
- More quickly get to market
- Less of what you don't need
- Minimize the amount of work not done
- Django major features:
- Amazing Documentation
- Python is easy language to learn and is clean (DRY, TDD, PEP08, etc)
- Auto generated Admin console
- ORM built in (and easy to use)
- TDD
- Demonstrate the value Django can bring inside your organization
- Overall Goal of this session:
-
- We only have 2 hours so we will be using Docker to spin things up quickly
- Allows us to skip python & django installation (since it will be done inside the Docker container)
- We will rely heavily on github repo sample pre-built before the class for code snippets
- What is covered:
- Project creation (using docker)
- Understand Django Project vs Apps
- ORM: Creating Models and Database Objects
- CRUD: Using the Admin to Create, Read, Update, & Delete our data
- Using TMV to create Django HTML Templates
- Python Shell, for exploring data in DB
- Web Forms in Django Templates
- We only have 2 hours so we will be using Docker to spin things up quickly
-
- Ensure Docker is running on your machine
- Open gitbash and run the docker command found here in the folder you want to create your code in:
- https://hub.docker.com/r/mchughmk/django-starter
- Name the project EventManager do not use - in the name
- CD into your new folder EventManager and execute this command:
-
%>docker-compose up -d
-
- Navigate to https://localhost:8000
- Verify you are seeing the Django project start up page
- Open code using VS Code
-
%>code .
-
-
- Executing commands inside the container shell:
-
%>docker-compose exec web bash OR %>docker-compose exec web whoami
-
- Other usefull commands
-
%>docker-compose logs -f web %>docker-compose start %>docker-compose stop %>docker-compose down %>docker-compose web --build
-
- Executing commands inside the container shell:
-
- Discuss manage.py
- Discuss difference in Projects and Apps in Django
- Initial project already created thanks to docker:
- See command in docker file
- django-admin.py startproject projectname
-
- I was told there would be a free admin console?
- First we must create a super user in the db:
-
%>docker-compose exec web bash (bash)%>python manage.py createsuperuser Follow prompts
- Navigate to your app /admin:
- https://localhost:8000/admin
- Notice there is already "Groups" & "Users"
-
-
- Must create an app inside our Django project.
- You can have multiple apps but today we will just have one
-
(bash)%>python manage.py startapp events
-
- Connect events app to project
- Add the below line to the "INSTALLED_APPS" section of the settings.py file
- 'events.apps.EventsConfig'
- Add the below line to the "INSTALLED_APPS" section of the settings.py file
- SEE branch: release-one-projectapp-creation
-
- Create Models:
- Event(models.Model)
- Name
- Date_time
- Description
- Enabled
- Person
- Name
- Attendance
- Person
- Event
- Event(models.Model)
- See (Model.py code to copy/paste): Click Me
- Create Models:
-
- Discuss ORM
-
%> python manage.py makemigrations
-
%> python manage.py migrate
-
%> python manage.py migrate 0003 (targeted migration "roll back")
-
- Look at migrations folder
- Review 00001_initial.py file it created
- SEE branch: release-3-projectapp-models
- Discuss ORM
-
- Register models in admin.py:
- Edit events/admin.py:
- Refresh admin
- Discuss Admin consoles
- SEE branch: release-6-projectapp-templates
- Register models in admin.py:
-
- Discuss MVC vs TMV
- Django is not true MVC, but it is equally as optimal
- Compareing the two:
- Template is view
- View is controller
- Model is model
- Urls.py maps the route
- Discuss MVC vs TMV
-
- Create events/urls.py:
from django.conf.urls import url from . import views app_name = 'events' urlpatterns = [ url(r'^\$', views.index, name='index'), # url(r'^(?P<event_id>[0-9]+)/$', views.detail, name='detail'), ] - Connect EventManager/urls.py to EventManager/events/url.py
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^events/', include('events.urls')), ]
- Create events/urls.py:
-
- Create basic "Hello World" in views.py
- Ensure "HttpResponse" has been imported in the top "from ..." line
- Add below code
def index(request): return HttpResponse('Hello World') - SEE branch: release-2-projectapp-view
- Create basic "Hello World" in views.py
-
- Connect view.py to templates:
- Add linked code to view.py Just the function def index
- SEE branch: release-4-projectapp-templates
- Connect view.py to templates:
-
- Discuss shell
%>python manage.py shell from events.models import Event, Person, Attendance Event.objects.all() e = Event(name='event one', description='this is the description') e.save() e.date_time e = Event(name='event two', description='this is the description for event two') e.save() Event.objects.filter(name__endswith='two') quit() <- to quit
- Discuss shell
-
- Discuss Forms:
- Create RegisterForm(forms.Form)
- Create events/forms.py file
-
- Create form and detail.html:
- Events/templates/events/details.html
- Uncomment line un urls.py so details route will work
- Update views.py to look like this:
- Use the form to create one or more attendance records in db
- SEE branch: release-5-projectapp-forms
-
- Q & A
- What next:
- Resources:
- https://www.djangoproject.com/
- View both the tutorial & overview
- Book: "Two Scoops of django" by Daniel Greenfeld and Audrey Roy
- https://www.djangoproject.com/
