0
def xyz():    
site_data=site.objects.all()
site_fields = site._meta.get_fields()
site_fields_name = [f.name for f in site_fields][1:]
return render(request, "Demoapp/InputGssA1.html", {'headers': table_structure,'site_data':site_data,'site_fieldname':site_fields_name})

this is i am render from function inside view.py and want to show in table format in InputGssA1.html, here table code is like

{% for i in site_data %}
<tr>
{% for x in site_fieldname %}
    <td>{{i.x}}</td>
{% endfor %}
</tr>

{% endfor %}

but problem is I can't access object.fieldname using i.x in my code , i.fieldname statically work but this way it not working, How I access object's fieldname this way inside template tag??

jv02
  • 21
  • 6
  • Does this answer your question? [Performing a getattr() style lookup in a django template](https://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template) – wfehr Apr 07 '20 at 07:08

1 Answers1

1

You don't need such complicated solution. You can simply try with values():

# view
def xyz(request):
    site_fields = site._meta.get_fields()
    site_fields_names = [f.name for f in site_fields][1:]
    site_data=site.objects.values(*site_fields_name)
    return render(request, "Demoapp/InputGssA1.html", {'headers': table_structure,'site_data':site_data})

# template
{% for data in site_data %}
{% for field, value in data.items %}
<tr>
    <td>{{ value }}</td>
</tr>
{% endfor %}
{% endfor %}
ruddra
  • 50,746
  • 7
  • 78
  • 101