pjlorenz
pjlorenz

Reputation: 3

How to display the user ID in a Django table

I am creating a site with a Forum type of format, where users can create Posts-- where they make statements or ask questions, etc. Then under the Posts, other users can create comments to this Post and carry on the discussion, or whatever the case may be.

The problem I'm having is when it comes to creating pages that show all the Posts, according to the User that created them.

I will show you the code that I have below: the second link with the tags is not linking me to the Posts created by that particular user, but always shows the same content from User #1 (the admin user). Can you tell me what I'm doing wrong that doesn't allow this to work? Thanks.

                {% for post in posts %}
                <tr>
                    <td>{{ post.postTitle }}</td>
                    <td>{{ post.postBody }}</td>
                    <td>{{ post.user }}</td>
                    <td><a href="{% url 'single_post' post.id %}" class="btn btn-info">View Post</a></td>
                    <td><a href="{% url 'user_post' user.id %}" class="btn btn-info">User's Posts</a></td>
                </tr>

                {% endfor %}

Example of Posts page

Upvotes: 0

Views: 303

Answers (1)

Konstantin K.
Konstantin K.

Reputation: 335

This is the problem

{% url 'user_post' user.id %}

With django.contrib.auth.context_processors.auth enabled, user refers to the authenticated user which, sounds like in your case, is admin. That why it goes to the admin user posts.

You're looking for post.user instead. To fix, change which user you're getting an id from, like this

{% url 'user_post' post.user_id %}

Upvotes: 1

Related Questions