Home > Blockchain >  Django for loop to display all objects in database
Django for loop to display all objects in database

Time:11-02

def homepage(response):
    data = Project_types.objects.all()
    return render(response, 'main/homepage.html',{'projects':data})


def hacks(response):
    return render(response, 'main/hacks.html', {})

def games(response):
    return render(response, 'main/games.html', {})

All I need to know is how to iterate through each object in the variable "data" in html. I want it displayed in the most simple way possible !

CodePudding user response:

Using the FOR template tags you can iterate through the objects in the data list ...

templatename.html:

{% for iteratorname in projects %}
    <h1> {{ iteratorname.attributes }} </h1>
{% endfor %}

Please read the documentation for more details

CodePudding user response:

You can use for template tag:

{% for project in projects %}
    <p> {{project.attributes}} </p>
{% endfor %}
  • Related