google app engine - Iterating over model attributes when creating a template in Django -
I am using Django in Google App Engine. If I have a class
class person (): first_name = stringProperty () last_name = StringProperty ()
And I have an example where the person. First_name = bob and Person.last_name = Vance, can I create a template that repeats on individual attributes for production:
& lt; Tr & gt; & Lt; TD & gt; First & lt; / TD & gt; & Lt; TD & gt; Bob & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; Last & lt; / TD & gt; & Lt; TD & gt; Vance & lt; / TD & gt; & Lt; / TR & gt; Perhaps more precisely, there is a model .as_table () method that will print the properties of my example, Bob Vance?
You can not use __underscored__ attributes or functions in the template. I suggest that you create a function in your model / class:
class.model: first_name = models.charphield (max_length = 256) last_name = models.CharField (max_length = 256) Def attrs (self): for attr, self. Value in Itteritems (): yield attr, value def, sorted_ector (self): The proxy example of return for # key [[key, self .__ word [__ [key]) sorted (self .__ dict__)]
This template is just for:
& lt; Tr & gt; {% For name, person value in person.}} & Lt; Td> {{Name}} & lt; / Td> & Lt; TD & gt; {{}} Price & lt; / TD & gt; {% Endfor%} & lt; / Tr & gt;
Now it will give you "first_name" instead of "first", but you get this idea. You can increase the method as a mix, or you can be present in a parent class etc. Similarly you can use it if you have some person objects that you want to redo:
{% of people%} & lt; Tr & gt; {% For name, person value in person.}} & Lt; Td> {{Name}} & lt; / Td> & Lt; TD & gt; {{}} Price & lt; / TD & gt; {% Endfor%} & lt; / Tr & gt; {% Endfor%}
Comments
Post a Comment