day$5:$ $django$models,$views,$and$...
TRANSCRIPT
GSL South Africa 2015
Today’s Meet-‐up
• Back to the big picture • Review of Models • Templates • Views • Today’s Assignment
2
GSL South Africa 2015
Models
5
View
Template Model
Send request for data
Asks the model for data
Provide Database InformaLon UI Layout Communicate
with Database
Handles InformaLon Exchange
GSL South Africa 2015
What is a Model
• Python class describing data in your applicaLon – Subclass of models.Model
• Assigns aUributes to each data field • Avoid direct work with the database – No need to handle database connecLons, Lmeouts, etc. – Let Django do it for you!
– Provides Schema for database
6
GSL South Africa 2015
Django Model Syntax
7
Import statements
SubClass of models.Model Class
Define fields
Can define more funcLons
__unicode__ corresponds to python __str__
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
8
Define Type of Field • E.g. models.CharField
Define arguments of field • E.g. max_length=200
Django will automaLcally create fields in database
GSL South Africa 2015
Important Django Field Types • BooleanField
– Checkbox • CharField(max_length)
– Single-‐line textbox • DateField
– Javascript calendar • DateTimeField
– Javascript calendar, Lme picker • DecimalField(max_digits, decimal_places)
– Decimal numbers • EmailField
– Charfield that validates email address
9
• FileField • File upload, stores path
in database • FloatField
• FloaLng point numbers • IntegerField
Integer textbox • PosiLveIntegerField
• Integer textbos for posiLve integers
• TextField • MulL-‐line textbox
GSL South Africa 2015
Copy Day 4’s code
• Make sure you are in an empty directory • echo "# gsl-django-demo" >> README.md• git init• git add README.md• git commit -m "first commit"• git remote add origin https://github.com/emzhang/gsl-django-demo.git
GSL South Africa 2015
Databases
ID First name Email Profile
photo …
1 Emily [email protected]
2 Soga [email protected]
3 Anita [email protected]
GSL South Africa 2015
Databases
ID User ID Post Text
1 1 Computer Science rules!
2 3
I love that all my students are so talkaLve and ask quesLons in class
GSL South Africa 2015
Facebook responds with posts JSON: [{ post: { id: 1, text: ’Computer Science rules!' }}, { post: { id: 5, text: ‘Does anyone have recommendations for good vegetarian food in…' }}]
GSL South Africa 2015
Templates
23
View
Template Model
Send request for data
Asks the model for data
Provide Database InformaLon UI Layout Communicate
with Database
Handles InformaLon Exchange
GSL South Africa 2015
What is a template?
• A text-‐based template for HTML, CSS, XML, JavaScript, etc.
• Defines the Layout of your Webpage in [name].html in your template directory
• Can contain – Hard coded text – Variables: {{ ... }} – Tags: {% ... %}
24
hUps://docs.djangoproject.com/en/1.8/topics/templates/
GSL South Africa 2015
Django Template Syntax
25
Hard coded text Tags: {% ... %} that control logic
Variables: {{ ... }} that get replaced
GSL South Africa 2015
Senng Variables
Templates by themselves are not very useful
How do we insert the content from our database into our UI?
Use Views to connect Models and Templates!
26
GSL South Africa 2015
Views
27
View
Template Model
Send request for data
Asks the model for data
Provide Database InformaLon UI Layout Communicate
with Database
Handles InformaLon Exchange
GSL South Africa 2015
What is a view
• Views are the logical interface between data (Models) and presentaLon (Templates)
• Defined in views.py inside the appFolder • EVERY view takes a request object as first parameter • EVERY view returns an HttpResponse object
28
from django.http import HttpResponsedef hello(request):
return HttpResponse("Hello world")
GSL South Africa 2015
Django View Syntax
29
Import statements
View definiLon: request object as first parameter
Create new variable of type Poll (defined in models)
Load template from polls/index.html
Create context: object containing dicLonary: Key == ‘latest_poll_list’ Value == latest_poll_list
Integrate context in template and return as HUpResponse
GSL South Africa 2015
DirecLng hUp requests to views
30
e.g. user requests hUp://[host]/polls/12/results
1.: look in [project]/mysite/urls.py (set in senngs.py)
2.: Direct to [project]/mysite/polls/urls.py
3.: Find url and load views.results
GSL South Africa 2015
Request Life Cycle
1. User requests to view URL 2. Django determines the root URLconf by looking at the
ROOT_URLCONF senng. 3. Django looks at all of the URLpaUerns in the URLconf
for the first one that matches the URL 4. If it finds a match, it calls the associated view funcLon. – Repeats Steps 3-‐4 if redirected to another urls.py
5. The view funcLon returns an HUpResponse. 6. Django converts the HUpResponse to the proper HTTP
response, which results in a Web page.
31