开发者

Django : Listing model field names and values in template [duplicate]

开发者 https://www.devze.com 2023-02-17 14:57 出处:网络
This question already has answers here: 开发者_开发知识库 Closed 11 years ago. Possible Duplicate:
This question already has answers here: 开发者_开发知识库 Closed 11 years ago.

Possible Duplicate:

Django - Iterate over model instance field names and values in template

Hi,

I'm trying to list fields and corresponding values of generic Django models in the templates. However I can't find an inbuilt solution for a fairly common problem. I'm pretty close to the solution but can't find a way out.

view.py Code:

def showdetails(request, template):
    objects = newivr1_model.objects.all()
    fields = newivr1_model._meta.get_all_field_names()
    return render_to_response(template, {'fields': fields,'objects':objects},
        context_instance=RequestContext(request))

template code:

    <table>                                                                                                                                       
    {% for object in objects %}                                                 
        <tr>                                                                    
            {% for field in fields %}                                           
                <td>                                                            
            <!--    {{ object.field }} /*This line doesn't work*/ -->                                           
                </td>                                                           
            {% endfor %}                                                        
        </tr>                                                                   
    {% endfor %}                                                                
    </table>

What should I be doing at the commented template line so that i get the value of Object.field?

Any better DRY methods are most welcome as well.


Unfortunately, you can't do lookups like that in the template engine.

You'll have to deal with that in the view.

def showdetails(request, template):
    objects = newivr1_model.objects.all()

    for object in objects:
        object.fields = dict((field.name, field.value_to_string(object))
                                            for field in object._meta.fields)

   return render_to_response(template, { 'objects':objects },
                                    context_instance=RequestContext(request))

Template

{% for object in objects %}
    <tr>
    {% for field, value in object.fields.iteritems %}
        <td>{{ field }} : {{ value }}</td>
    {% endfor %}
    </tr>
{% endfor %}


You need to create your own filter, that will work like getattr in python and use it in the template:

{{ object|getattribute:field }}

Here there is description how to do that: Performing a getattr() style lookup in a django template

But I don't think that's really a good idea. Insted try to do this logic in the view, like this:

object_values = []
for object in objects
   object_values.append([])
   for field in fields:
       object_values[-1].append(getattr(object, field))
return render_to_response(template, {'object_values': object_values},
        context_instance=RequestContext(request))

and in the template:

<table>                                                                                                                                       
{% for values in object_values %}                                                 
    <tr>                                                                    
        {% for value in values %}                                           
            <td>                                                            
                {{ value }}
            </td>                                                           
        {% endfor %}                                                        
    </tr>                                                                   
{% endfor %}                                                                
</table>

Django template system doesn't provide many function (filters), because you are supposed to do all the logic in the views. Template should only present data.

0

精彩评论

暂无评论...
验证码 换一张
取 消