GitHub - atuggle/Django-Startup-Project-Tutorial: 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 · GitHub
Skip to content

atuggle/Django-Startup-Project-Tutorial

Folders and files

Repository files navigation

Django-Startup-Project-Tutorial

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

Updated to use Docker (removed CodeAnywhere)

  1. Slide (Overivew):

    • 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

  1. Slide (Getting started):

    • 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

  1. Slide (Getting the code up and running!):

    1. Ensure Docker is running on your machine
    2. Open gitbash and run the docker command found here in the folder you want to create your code in:
    3. CD into your new folder EventManager and execute this command:
      • %>docker-compose up -d

    4. Navigate to https://localhost:8000
    5. Verify you are seeing the Django project start up page
    6. Open code using VS Code
      • %>code .


  1. Slide (Working with the Docker Container):

    • 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


  1. Slide (%> python manage.py):

    • 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

  1. Slide (Admin Console):

    • 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:

  1. Slide (Create events app):

    • 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'
    • SEE branch: release-one-projectapp-creation

  1. Slide (Create db models for ORM):

    • Create Models:
      • Event(models.Model)
        • Name
        • Date_time
        • Description
        • Enabled
      • Person
        • Name
        • Email
      • Attendance
        • Person
        • Event
    • See (Model.py code to copy/paste): Click Me

  1. Slide (ORM migrations):

    • 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

  1. Slide (Free Admin CRUD pages for models):

    • Register models in admin.py:
      • Edit events/admin.py:
      • Refresh admin
      • Discuss Admin consoles
    • SEE branch: release-6-projectapp-templates

  1. Slide (MVC vs Django's TMV):

    • 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

  1. Slide (urls.py & Routes):

    • 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')),
       ]
      

  1. Slide (Test route is working):

    • 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

  1. Slide (HTML Templates!):

    • Create Templates and connect to view:
      • Discuss why duplication of app name in template folder
      • events/templates/events/index.html
      • events/templates/base.html
      • Template inheritance via {% block name %} [% endblock %}
      • Events/static/events/starter-template.css

  1. Slide (Connect our view.py (controller) to our html templates):

    • Connect view.py to templates:
      • Add linked code to view.py Just the function def index
    • SEE branch: release-4-projectapp-templates

  1. Slide (python shell for data evaluation):

    • 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
      

  1. Slide (Web Forms & Django Templates):

    • Discuss Forms:
    • Create RegisterForm(forms.Form)

  1. Slide (Web Forms continued):

    • 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

  1. Slide (What's next?):

    • Q & A
    • What next:
    • Resources:

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors