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??

1

1 Answer 1

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 %}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks , it work and if I am not want to show first column value then?
in values parameter, you can define which fields you want to use, ie .values('name', 'area')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.