Day 4: Introduc%On to Django
Total Page:16
File Type:pdf, Size:1020Kb
Day 4: Introduc/on to Django GSL South Africa 2015 Today’s Agenda • The Big Picture • Django – A Web Applicaon Framework • Your First Django App • Models in Django • Today’s Assignment 2 GSL South Africa 2015 The Big Picture 3 GSL South Africa 2015 Once upon a /me… OR 4 GSL South Africa 2015 Nowadays… OR OR GSL South Africa 2015 Implemen/ng the Backend We need to implement the backend: • A web applicaon framework, like – MonoRail, CppCMS, Apache Click, Grails, Spring, Stripes, mul/ple, Catalyst, CakePHP, Drupal, Symfony, CherryPy, Django, web2py, Ruby on Rails, Compujure • A host, like – Heroku, Google App Engine • A RESTful API to communicate with frontend 6 GSL South Africa 2015 Web Applicaon Framework • A framework (code libraries) to help you make web applicaons or websites • Supports you with – Handling HTTP requests – Templates for common HTML layouts – URL mapping – Database communicaon – Session management – Site security 7 GSL South Africa 2015 Django Web Applicaon Framework 8 Why Django? GSL South Africa 2015 Why Django? • Fast and easy development of web applicaons – Modular and re-useable. Don’t Repeat Yourself (DRY) principle – Built-in database management • Ac/ve development and wide community support • Supported by Google App Engine & Heroku 10 Django vs. PHP GSL South Africa 2015 PHP SELECT Statement GSL South Africa 2015 Django SELECT Statement Django Architecture GSL South Africa 2015 Model-View-Controller (MVC) • A paern for organizing code oeen seen in web app frameworks • Main idea: 1. Separate storage and manipulaon of data (model) from presentaon of data (view) 2. Controller communicates between model and view • Advantages – Develop and test model and view independently – Easier for others to understand (modularity) 16 GSL South Africa 2015 Model-View-Controller (MVC) (news site example) Controller Asks the model for Send request for the story and its a story user comments View Model Serves requested • Layout of stories story • News stories and on mobile phone or images in a database desktop browser • User comments 17 GSL South Africa 2015 Model-Template-View In Django: Model-Template-View (similar to MVC paern) • Model – describes database informaon • Template – decides how to present informaon • View – manages what informaon to output based on request 18 GSL South Africa 2015 Model-Template-View View Handles Asks the model for Send request for Informaon Exchange data data Template Model Provide Database UI Layout Informaon Communicate with Database 19 GSL South Africa 2015 Your First Django App 20 GSL South Africa 2015 Programming Interface Terminal / Command Prompt Python IDLE or Favorite editor 21 GSL South Africa 2015 New Project Project Structure: Whole project in one folder (mysite) • MySite Python package • MySite Python Package • Applicaons (polls) • __init__.py • (Database) • sengs.py • manage.py • urls.py • Use for interac/on with your • wsgi.py project 22 GSL South Africa 2015 First Django Setup Aeer a couple commands in terminal... 23 Models 24 GSL South Africa 2015 Models View Handles Asks the model for Send request for Informaon Exchange data data Template Model Provide Database UI Layout Informaon Communicate with Database 25 GSL South Africa 2015 What is a Model • Python class describing data in your applicaon – Subclass of models.Model • Assigns aributes to each data field • Avoid direct work with the database – No need to handle database connec/ons, /meouts, etc. – Let Django do it for you! – Provides Schema for database 26 GSL South Africa 2015 Django Model Syntax Import statements SubClass of models.Model Class Define fields __unicode__ corresponds to python __str__ Can define more funcons 27 GSL South Africa 2015 Django Fields We can define fields directly in our model class – No need to define manually in database Example: create two fields in our Poll class Define Type of Field Django will • E.g. models.CharField automacally create Define arguments of field fields in database • E.g. max_length=200 28 GSL South Africa 2015 Important Django Field Types • BooleanField • FileField – Checkbox • File upload, stores path in database • CharField(max_length) • FloatField – Single-line textbox • Floang point numbers • DateField • IntegerField – Javascript calendar Integer textbox • DateTimeField • Posi/veIntegerField • – Javascript calendar, /me picker Integer textbos for posi/ve integers • DecimalField(max_digits, decimal_places) • TextField – Decimal numbers • Mul/-line textbox • EmailField – Charfield that validates email address 29 GSL South Africa 2015 Relaonships • Clearly, the power of relaonal databases lies in relang tables to each other. Django offers ways to define the three most common types of database relaonships: many-to-one, many-to-many and one-to-one. GSL South Africa 2015 Relaonship Examples: Many-to-One • A Car has a Manufacturer, but a Manufacturer can have many Cars GSL South Africa 2015 Relaonship Examples: Many-to-Many • A Pizza can have mul/ple Toppings. Each type of Topping can be on mul/ple Pizzas GSL South Africa 2015 Relaonship Examples: Many-to-Many • Some/mes you need to associate data with the relaonship between a Many-to-Many. GSL South Africa 2015 Relaonship Examples: One-to-One GSL South Africa 2015 Rules of Django Models • When you update a model, ALWAYS RUN python manage.py migrate • All classes extend models.Model • Models only live in Apps • Django doesn't save objects un/l you call save() method >>>a1 = Album(...) # a1 is not saved to the database yet! >>>a1.save() # Now it is. 35 GSL South Africa 2015 Today’s Assignment Get started with Django! – If you haven’t already, install Django 1.8 – Create your first Django app: Wri/ng your first Django App – Part 1 Wri/ng your first Django App – Part 2 – Come up with preliminary data model for you project Helpful Documentaon: – Models sec/on of Django docs 36 .