1 of 16

Section 7

Django

materials at jrsacher.github.io/web50

2 of 16

Outline

  • Project 1 post-mortem
  • Project 2 notes/questions
  • Project 3 details
  • Django

3 of 16

Project 1

  • Grades/comments will be released before Project 2 is due
  • Some common issues
    • Lack of comments
    • Lack of error handling
    • Focusing on looks before specifications

4 of 16

Project 2

  • Due Monday 7/22 at noon
  • Advice
    • Make liberal use of console.log() and print() in debugging
      • remove before submitting
    • Spend time thinking about how to best store channel/message info
    • Don't try to make everything work at once
      • Break problem down into small pieces, then break those down as well
      • If stuck, try writing out pseudocode as comments, then filling in actual code
    • Use the readme.md file to your advantage
    • Plan for a user that's trying to break things

5 of 16

Project 3

  • Last structured project!
  • All critical material was covered in Lecture 7
  • Important to stay organized, work in small steps
  • Spend time planning, diagramming, etc.
    • Especially your DB!
  • Due Wednesday 7/31 at noon (but don't wait!)

6 of 16

A note on style

Some places to start:

  • PEP 8, Python's official style guide
  • AirBnB and Google JS style guides
  • W3Schools and Google for HTML

Style is not just looks!

  • appropriate, meaningful variable names
  • appropriate comments in the code
  • etc.

7 of 16

Django

Flask

  • "Microframework"
  • Lightweight
  • SQLAlchemy needed for DBs
  • Single application per project
  • Jinja2 for templating
  • Users
    • Netflix
    • Reddit
    • Lyft AND Uber

Django

  • "Full-stack"
  • Tons of built-in tools
  • DB access central to framework
  • Multiple apps in one project possible
  • Django templating language
  • Users
    • YouTube
    • Instagram
    • Spotify

Installation: pip install Django

Django documentation

8 of 16

Creating a project

django-admin startproject <projectname>

projectName/

manage.py

projectName/

__init__.py

settings.py

urls.py

wsgi.py

(python scripts to run app)

(python packages)

(settings: ex: timezone, database)

(routing for entire project)

(for web deployment)

Slide: Elle Buellesbach

9 of 16

Applications

django-admin startapp <appname>

projectName/

manage.py

projectName/

__init__.py

settings.py

urls.py

wsgi.py

appName/

__init__.py

urls.py

view.py

models.py

AND OTHERS!

(the high level project controls)

(URLs will be routed through here first)

(an individual app within the project)

(Associating URLs with view functions)

(functions that run to create responses)

Slide: Elle Buellesbach

10 of 16

Convert a Flask app to Django

Protein identity matrix calculator from UniProt IDs

https://cadd-cdot.appspot.com/identity

Some test data:

P00533 P04626 P21860 Q15303

Can take a bit to run -- not optimized yet!

Not guaranteed to be up forever. On a public Google Cloud instance for development.

11 of 16

Django databases

  • Database models go in models.py
    • Similar to Flask-SQLalchemy ORM (lecture 4)

python manage.py makemigrations

python manage.py migrate

12 of 16

Interacting with the DB

  • Python shell
    • python manage.py shell
    • Interactive Python session
  • Django admin app
    • Make a superuser (python manage.py createsuperuser)
    • Register models in admin.py (admin.site.register(<model>))
    • Run server and go to http://127.0.0.1:8000/admin

13 of 16

Django user forms

  • Easy to do! A built in feature
    • from django.contrib.auth import authenticate, login, logout
  • Brian demoed manual user creation
  • Lots of resources out there that show how to make a login form
    • Like this one

14 of 16

15 of 16

16 of 16