Vivacious Views
Vivacious Views
View functions take as input: HttpResponse object
– contains useful information about the client,
sessions, etc.
Return as output HttpResponse object – basically
HTML output
Most often, views don't actually write HTML – they
fill in
templates
! (discussed shortly)
Vivacious Views
Example view:
from django.shortcuts import render_to_response
from mysite.polls.models import Poll
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
return render_to_response('polls/index.html', {'poll_list': poll_list})
Templates
Templates, Tags, & Tricks
The Django
template
language
consists of
tags
, which perform
many functions and may be
embedded in a text file to do neat
tricks
.
Templates, Tags, & Tricks
Tags:
Ex. variable
{{ poll.question }}
Ex. for-loop
{% for choice in poll.choice_set.all %}
…
{% endfor %}
Ex. if-statement
{% if patient_list %}
...
{% else %}
...
{% endif %}
Templates, Tags, & Tricks
Example:
Displaying poll results
{{ poll.question }}
{% for choice in poll.choice_set.all %}
- {{ choice.choice }} -- {{ choice.votes }}
vote{{ choice.votes|pluralize }}
{% endfor %}
|