How to send dict to template and use it

4 views
Skip to first unread message

Mohsen Pahlevanzadeh

unread,
May 7, 2020, 1:56:00 AM5/7/20
to django...@googlegroups.com
I have the following view function:
################################
def index(request, question_id):
latest_question_list = User.objects.all() #Post.objects.all()
template = loader.get_template('posts/index.html')
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context))
#########################################
And the following template:
########################################
{% if latest_question_list %}

salaam
<a href="/posts/{{ question.id }}/">{{ latest_question_list }}</a>
{% endif %}
###########################################

I see the following result in my browser:
#############################################

salaam
<QuerySet [<User: m_pahlevanzadeh>]>


##########################################

How Can I some field in my Queryset in template?

Amitesh Sahay

unread,
May 7, 2020, 2:18:05 AM5/7/20
to django...@googlegroups.com
Your view seems to have incorrect render.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsub...@googlegroups.com.

Andréas Kühne

unread,
May 7, 2020, 4:57:27 AM5/7/20
to django...@googlegroups.com
Hi,

If you want to see something in the queryset you need to query it :)

For example:

 <a href="/posts/{{ question.id }}/">{% for question in latest_question_list %} {{question.id }}{%endfor%}</a>

Or you can take the last or first item?
 <a href="/posts/{{ question.id }}/">{{ latest_question_list.first.id }}</a>

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages