El Mon, 21 Sep 2009 08:43:57 -0700 (PDT)
Paul Lundberg <[email protected]> escribió:

> Hi:
> I have several Django applications running and I am trying to tidu up
> some code.
> 
> I am trying to make a generic way of viewing a database table on a web
> page using a YUI control.
> 
> I am using the Django template to attempt to create the required
> javascript for the YUI controls.
> 
> I become a bit stuck, in that I can't seem to iterate over the fields
> in a record in a Django template.
> 
> For example I have a project table as follows:
> 
> class projects(models.Model):
>     projectName                       = models.CharField('Project Name',
> max_length=50, null=False)
>     projectDescription        = models.TextField('Project Description',
> null=True)
>     projectManager            = models.CharField('Project Manager',
> max_length=50, null=True)
>     projectOwner              = models.CharField('Project Owner',
> max_length=50, null=True)
>     projectDate         = models.DateField('Project Date',
> null=False)
>     projectHidden             = models.BooleanField('Project Hidden',
> null=True)
> 
> Then there is a Django view that is used to get the data to render, in
> that I get a list of all the projects:
> 
>  projList = projects.objects.all()
> 
> projList is then passed into the function that renders the page.
> 
> I can go through each record in the projList using:
> 
> {% for projectItem in projList %}
> :
> :
> {% endfor %}
> 
> However I can't seem to go through each field in an individual record
> for example this doesn't work:
> 
> {% for projectItem in projList %}
>     {% for fieldval in projectItem.fields %}
>         {{fieldval.name }} -:-
> {{ fiieldVal.str }}
>     {% endfor %}
> {% endfor %}
> 
> This doesn't cause an error but it doesn't produce any output.

Model instances are not iterable (forms are, they're different type of objects)

> I can access the fields using the field names
> 
> {% for projectItem in projList %}
>     {{ projList.projectName }}
> :
> {% endfor %}
> 
> This produces an output but this gets away from the point of making a
> generic function..
> 
> Is this possible? Is this because the fields are not bound to a form's
> fields?
> 
> Any help or advice would be greatly apprecriated!

You could write a method that outputs what you want and call it from the
template.

-- 
P.U. Gonzalo Delgado <[email protected]>
http://gonzalodelgado.com.ar/

Attachment: pgpvnWYhmhVwf.pgp
Description: PGP signature

Reply via email to