11. class HttpResponseRedirect class HttpResponsePermanentRedirect class HttpResponseNotModified class HttpResponseBadRequest class HttpResponseNotFound class HttpResponseForbidden class HttpResponseNotAllowed class HttpResponseGone class HttpResponseServerError
12.
13.
14. In [1]: from django.template import Template # Parsear código de Templates y generar una instancia In [4]: t = Template("Hello, {{ name }}") In [6]: print t <django.template.Template object at 0x87c7aec> In [8]: from django.template import Context # Una vez compilada podemos renderizar con un contexto In [9]: c = Context({'name' : 'World'}) In [10]: t.render(c) Out[10]: u'Hello, World' Plantillas
15. {% extends "base_for_examples.html" %} {% block title %} Un proyecto: {{ project.name }} {% endblock title %} {% block content %} <p> <b> Un proyecto: </b> </p> <hr/> <p> Nombre: {{ project.name|upper }} </p> <p> Descripción: {{ project.description|truncatewords:"10" }} </p> {% endblock content %} Variable, el punto (.) se accede a los atributos de la variable, diccionario, llamada a una función sin arg. Etiqueta que define un bloque que se puede sobre escribir. Herencia. Filtro, módifica el string, letras mayúsculas Indica de que template hereda Plantillas TEMPLATE_STRING_IF_INVALID
16.
17.
18. {% if project_list %} {% for project in project_list %} <li> <a class="head" href='#'>{{project.name}}</a> <ul> {% for task in project.task_set.all %} <li> <a href="#" onclick="retrieve_tasklogs({{ task.id }})"> {{ task.name }} </a> </li> {% endfor %} </ul> </li> {% endfor %} {% else %} <li> <a class="head" href='#'> No projects in database </a> Please add some projects in the database </li> {% endif %} Plantillas
40. AJAX def example_ajax_get_task_for_project (request, project_id): if request.method == 'GET': p = get_object_or_404(Project, pk=project_id) tasks = p.task_set.all() if request.is_ajax(): # HTTP_X_REQUESTED_WITH # with string 'XMLHttpRequest' return render_to_response('task_details_ajax.html', { 'tasks' : tasks }) else: return render_to_response('project_with_tasks_details.html', { 'project' : p, 'tasks' : tasks }) <!-- La plantilla --> <ul> {% for task in tasks %} <li> Nombre: {{ task.name }} </li> <li> Descripción: {{ task.description }} </li> <li> Fecha de Inicio: {{ task.start_date }} </li> <!-- plin, plin, plin .. --> {% endfor %} <ul>
41. Serialización In [11]: from django.core import serializers In [12]: data=serializers.serialize("xml",Project.objects.filter(pk=1), fields=('name',)) In [17]: data Out[17]: '<?xml version="1.0" encoding="utf-8"?><django-objects version="1.0"><object pk="1" model="timetracker.project"><field type="CharField" name="name">Curso Django</field></object></django-objects> In [18]: for obj in serializers.deserialize("xml", data): ....: print obj, type(obj) ....: ....: <DeserializedObject: Curso Django> <class 'django.core.serializers.base.DeserializedObject'>
42.
43.
44. This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 2.5 Argentina License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ar/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.