whitebear
whitebear

Reputation: 12433

Use models in template django 3

I want to show the list of items in MyModel.

I used to use php framework , not good for python yet.

I googled around and search for best practices, but I cant find the explanation for Django 3.

Does anyone help?

my code is like this.

in views.py

mymodels = MyModel.objects.all()

data = {
    'mymodels' : mymodels
}
return render(request, 'myapp/index.html',data)

in myapp/index.html

{% for m in mymodels %}
{{m.id}}
{% endfor %}

Upvotes: 0

Views: 40

Answers (1)

amdev
amdev

Reputation: 7442

you are doing the right way in your html file, that's how it goes...

{% for item in mymodels %}
  <h2>{{ item.field_name }}</h2>
{% endfor %}

Upvotes: 2

Related Questions