6. Language Review
• Variables come from a context
• Dictionary-like name-value mapping
• A context is rendered by the template
• Variable replacement
• Template tag execution
21. from django.shortcuts import render_to_response!
from django.template import RequestContext!
!
def custom_proc(request):!
return {!
'app': 'My app',!
'user': request.user,!
'ip_address': request.META['REMOTE_ADDR']!
}!
!
def view_1(request):!
context = RequestContext(request)!
return render_to_response(!
'template1.html',!
{'message': 'I am view 1.'},!
context_instance=context!
)!
!
def view_2(request):!
context = RequestContext(request)!
return render_to_response(!
'template2.html',!
{'message': 'I am the second view.'},!
context_instance=context!
)
22. from django.shortcuts import render_to_response!
from django.template import RequestContext!
!
def custom_proc(request):!
return {!
'app': 'My app',!
'user': request.user,!
'ip_address': request.META['REMOTE_ADDR']!
}!
!
def view_1(request):!
context = RequestContext(request)!
return render_to_response(!
'template1.html',!
{'message': 'I am view 1.'},!
context_instance=context!
)!
!
def view_2(request):!
context = RequestContext(request)!
return render_to_response(!
'template2.html',!
{'message': 'I am the second view.'},!
context_instance=context!
)
23. from django.shortcuts import render!
!
def custom_proc(request):!
return {!
'app': 'My app',!
'user': request.user,!
'ip_address': request.META['REMOTE_ADDR']!
}!
!
def view_1(request):!
return render(!
'template1.html',!
{'message': 'I am view 1.'}!
)!
!
def view_2(request):!
return render(!
'template2.html',!
{'message': 'I am the second view.'}!
)
24. Custom Processors
• Do one thing and do it well
• Pick your variable names
•Context keys are global
• General import conventions apply
25. • Breaking down render_to_response
• RequestContext and context processors
• TEMPLATE_CONTEXT_PROCESSORS
• django.shortcuts.render
Summary
26. HTML Escaping
• Variable values are escaped by default
• {{ something|safe }}
• {% autoescape off %}
Not escaped: {{ something }}
{% endautoescape %}!
• Literals in tags/filters are not escaped
• {{ escaped|default:'1 < 2' }}
33. Before We Start...
• This is a HUGE topic
• Read the documentation
• Learn how others do it
• Template processing is string manipulation
• Slow, period
• No fancy things unless necessary
41. from django import template!
!
register = template.Library()!
!
!
def upper(value):!
"""Converts a string into all uppercase."""!
return value.upper()!
!
!
def add(value, arg):!
"""Adds the arg to the value."""!
return int(value) + int(arg)
42. from django import template!
!
register = template.Library()!
!
!
def upper(value):!
"""Converts a string into all uppercase."""!
return value.upper()!
!
!
def add(value, arg):!
"""Adds the arg to the value."""!
return int(value) + int(arg)
43. from django import template!
!
register = template.Library()!
!
!
def upper(value):!
"""Converts a string into all uppercase."""!
return value.upper()!
!
!
def add(value, arg):!
"""Adds the arg to the value."""!
return int(value) + int(arg)!
!
!
register.filter('upper', upper)!
register.filter('add', add)
44. from django import template!
!
register = template.Library()!
!
!
@register.filter(name='upper')!
def upper(value):!
"""Converts a string into all uppercase."""!
return value.upper()!
!
!
@register.filter(name='add')!
def add(value, arg):!
"""Adds the arg to the value."""!
return int(value) + int(arg)!
45. from django import template!
!
register = template.Library()!
!
!
@register.filter!
def upper(value):!
"""Converts a string into all uppercase."""!
return value.upper()!
!
!
@register.filter!
def add(value, arg):!
"""Adds the arg to the value."""!
return int(value) + int(arg)!
46. @register.filter!
def upper(value):!
"""Converts a string into all uppercase."""!
return value.upper()!
!
!
@register.filter!
def add(value, arg):!
"""Adds the arg to the value."""!
try:!
return int(value) + int(arg)!
except (ValueError, TypeError):!
try:!
return value + arg!
except Exception:!
return '' # Return something
47. @register.filter!
def upper(value):!
"""Converts a string into all uppercase."""!
return value.upper() # Let it fail!
!
!
@register.filter!
def add(value, arg):!
"""Adds the arg to the value."""!
try:!
return int(value) + int(arg)!
except (ValueError, TypeError):!
try:!
return value + arg!
except Exception:!
return ''
51. from django.conf import settings!
from django.utils import formats!
!
@register.filter!
def date(value, arg=None):!
"""Formats a date according to the given format."""!
if value in (None, ''):!
return ''!
if arg is None:!
arg = settings.DATE_FORMAT!
try:!
return formats.date_format(value, arg)!
except AttributeError:!
try:!
return format(value, arg)!
except AttributeError:!
return ''
52. from django.conf import settings!
from django.utils import formats!
!
@register.filter!
def date(value, arg=None):!
"""Formats a date according to the given format."""!
if value in (None, ''):!
return ''!
if arg is None:!
arg = settings.DATE_FORMAT!
try:!
return formats.date_format(value, arg)!
except AttributeError:!
try:!
return format(value, arg)!
except AttributeError:!
return ''
53. from django.conf import settings!
from django.utils import formats!
!
@register.filter!
def date(value, arg=None):!
"""Formats a date according to the given format."""!
if value in (None, ''):!
return ''!
if arg is None:!
arg = settings.DATE_FORMAT!
try:!
return formats.date_format(value, arg)!
except AttributeError:!
try:!
return format(value, arg)!
except AttributeError:!
return ''
54. from django.conf import settings!
from django.utils import formats!
!
@register.filter!
def date(value, arg=None):!
"""Formats a date according to the given format."""!
if value in (None, ''):!
return ''!
if arg is None:!
arg = settings.DATE_FORMAT!
try:!
return formats.date_format(value, arg)!
except AttributeError:!
try:!
return format(value, arg)!
except AttributeError:!
return ''
55. Return Something
•Not necessarily a string
•Only need to be convertible to a string
• None is converted to 'None', not ''
•I always return strings
83. Again...
• This is a HUGE topic
• Read the documentation
• Learn how others do it
• Template processing is string manipulation
• Slow, period
• No fancy things unless necessary