25. As mensagens poder guardadas em diversos meios (BD, cookies, sessões...)
26. Mensagens Code, code, code... from django.contrib import messages messages.debug(request, '%s SQL statements were executed.' % count) messages.info(request, 'Three credits remain in your account.') messages.success(request, 'Profile details updated.') messages.warning(request, 'Your account expires in three days.') messages.error(request, 'Document deleted.') {% if messages %} <ul class="messages"> {% for message in messages %} <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li> {% endfor %} </ul> {% endif %}
27. Consultas SQL Customizadas Django < 1.2 from django.db import connection from library.models import Author cursor = connection.cursor() query = "SELECT * FROM library_author" cursor.execute(query) results = cursor.fetchall() authors = [] for result in results: author = Author(*result) authors.append(author)
28. Consultas SQL Customizadas Django >= 1.2 from library.models import Author query = "SELECT * FROM library_author" authors = Author.objects.raw(query) >>> authors = Author.objects.raw("SELECT *, age(birth_date) as age FROM library_authors") >>> for author in authors: ... print "%s is %s." % (author.first_name, author.age) John is 37. Jane is 42. ...
35. Validação de dados from django.core.exceptions import ValidationError from django.db import models def validate_even(value): if value % 2 != 0: raise ValidationError(u'%s is not an even number' % value) class MyModel(models.Model): even_field = models.IntegerField(validators=[validate_even]) Code, code, code...
46. Templates Django < 1.2 {% ifequal somevar "x" %} This appears if variable somevar equals the string "x" {% endifequal %} {% ifnotqual somevar "x" %} This appears if variable somevar does not equal the string "x", or if somevar is not found in the context {% endifnotequal %}
47. Templates Django >= 1.2 {% if somevar == "x" %} This appears if variable somevar equals the string "x" {% endif %} {% if somevar != "x" %} This appears if variable somevar does not equal the string "x", or if somevar is not found in the context {% endif %} {% if you.friends.count > 5 %}You're popular!{% endif %} {% if country != "BR" %}Come and visit Brazil!{% endif %} {% if staff or author and not expired %} <a href="{{ edit_url }}">Edit this</a> {% endif %}