Reputation: 11
I need to display in template post and username which wrote this post. Model "post" have ForeignKey to user.
{% for post in post_list %}
<tr>
<td>{{ post.user_name }}</td>
<td>{{ post.user_name }}</td>
<td>{{ post.post_user.user_name }}</td>
<td>{{ post.post_content }}</td>
</tr>
{% endfor %}
class user(models.Model):
user_name = models.CharField(max_length=25)
class post(models.Model):
post_content = models.CharField(max_length=65000)
post_user = models.ForeignKey(user)
none of way which i tried does not work
Upvotes: 1
Views: 355
Reputation: 9346
<td>{{ post.post_user.user_name }}</td>
Should be the winner.
What is outputted for each of those options?
Try using the |pprint
filter to see what is going on:
{{ post|pprint }}
{{ post.post_user|pprint }}
Upvotes: 2
Reputation: 133
{{ post|pprint }}
{{ post.post_user|pprint }}
{{ post.post_user.user_name|pprint }}
{{ post|pprint }}
<post: post object>
<user: user object>
'1'
<post: post object>
that's my views
try:
post_list = post.objects.select_related().filter(post_thread_id = thread_obj.id).order_by('-post_date')
except:
post_list = False
c = {
'thread': thread_obj,
'post_list': post_list,
'user_login' : user_login,
'user_name' : user_name, // user name which is login
}
c.update(csrf(request))
return render_to_response("show_thread.html", c)
Upvotes: 1