Thursday, November 24, 2011

Re: converting to a template

On Thu, Nov 24, 2011 at 4:40 PM, marjenni <mark.jennings.email@gmail.com> wrote:
>
> I have a python script in which I am building up an html page, and
> then making a call to HttpResponse(html) to show the page.
>
>
> Now I want to start to using templates, but this is not as easy as I
> hoped because my html contains a table with a lot of information, and
> I don't know how to make function calls from the template.
>
> Are there any clear examples of how to dynamically create a table from
> within a template?
>

By design, you can't really make function calls from within
templates*. The reason for this is that if you do, your template is no
longer really a template, but more like a program or script.

Template languages are astonishingly bad at writing logic in, you end
up with confusing templates and hidden logic, so Django deliberately
forces you to do as much work as possible in your view, and pass in
the data required in a simple enough format to draw your view.

Eg, if you were outputting a table, you should generate your data as a
list of lists, and pass it to the template in the context. You can
then iterate through the list, generating a row for each list.

So, the real question is always "what data structures do I need to
massage my data into so that I can output it cleanly in my template".

Cheers

Tom

* This is not true, you can. But they are limited to functions which
do not take arguments, or by writing custom template filters or tags.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment