A
A
Altyn Bek2015-08-31 15:27:37
Django
Altyn Bek, 2015-08-31 15:27:37

How to display different models in one template?

Good day Django Experts!
Please tell me, I can't solve the problem:
- I want to show 2 news from each category on the main page similar to the picture below.
e5375e68c3d94a36bfbca2594b1fa65e.png
Tell me please, how do you implement such tasks? Maybe I'm looking bad, poke me the link)
PS.
For example, on the main page I want to display blocks: News / Vacancies / Announcements. I connect through include it is impossible.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
baterson, 2015-08-31
@baterson

In the question it is better to show your code and what exactly is the problem. Judging by the context, it's just not clear how to use the template language. To display model objects, they must be passed to the view function. And then write something like this in html:
{% for model in some_model %}
{{ model.some_method }}
{% endfor %}
If you want to display all models in different html files and then transfer them to base.html
Then put them in the template , like this { block news } content { endblock }
and then place it in base.html as you like, also through blocks { block news }{ endblock }

Z
zelsky, 2015-08-31
@zelsky

If I understand you correctly.
views.py

x = Model1.object.all()
y = Model2.object.all()
context = {'from_model_one': x ,'from_model_two': y }

template.html
{% for a in from_model_one%}
{{a.id}}
{{a.text}}
{% endfor %}
{% for b in from_model_two %}
{{b.id}}
{{b.text}}
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question