0

Edit: Here are my Views and My models and the Html.I am trying to make a user profile page in django. I need to query and and pull only the Info of the request.user in the profile page.The problem seems to be that I am not able to get the correct query filter.

views.py

 class UserView(generic.ListView):
     model = Post
     template_name = 'post/user_page.html'
     context_object_name = 'all_post'

     def get_queryset(self):
         return Post.author.all()

Models.py

class Post(models.Model):
    creator = models.CharField(max_length=250)
    post_name = models.CharField(max_length=250)
    category = models.CharField(max_length=250)
    post_photo = models.FileField()
    author = models.ForeignKey(User, blank=True, null=True, related_name ='user_post')
    category = models.ManyToManyField(Category)

    def get_absolute_url(self):
        return reverse('post:detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.creator + ' - ' + self.post_name

Profile Page

 <div class="col-sm-6 col-sm-offset-3">
    <div class="roote-container container-fluid">
        <h1>Welcome {{user.username}}</h1>
        <div class="row">
                {% for post in all_post %}
                    <div class= col-sm-12">
                        <div class="thumbnail">

                            <!--Post Photo-->
                            <a href="{% url 'post:detail' post.id %}" >
                                <img src="{{ post.post_photo.url }}" class="img-responsive">
                            </a>

                            <div class="caption">
                                <h2>{{ post.post_name }}</h2>
                                <p>{{ post.user }}</p>

                                <!-- Details-->
                                <a href="{% url 'post:detail' post.id %}"class="btn btn-primary">{{post.posts_name}} post</a>

                                 <!--delete-->
                                 <form action="{% url 'post:post-delete' post.id %}" method="post" style="display inline;">
                                    {% csrf_token %}
                                    <input type="hidden" name="post_id" value="{{ post.id }}"/>
                                    <button type="submit" class="btn btn-default btn-sm">
                                    <span class="glyphicon glyphicon-trash"></span>
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                {% endfor %}
        </div>
    </div>
</div>
Migdotcom
  • 72
  • 1
  • 11

2 Answers2

2

What is Post.author.all()?

If you need all the posts of a specific author then do

Post.objects.filter(author__username= 'name of author') 

Where name is a field in your Author model.

Or if you want all the posts from all the author

 Post.objects.all().select_related('author')
Exprator
  • 26,992
  • 6
  • 47
  • 59
  • Oh okay so it's best to have a separate author model ? For some reason I always thought the Import User took care of that. – Migdotcom Jul 10 '17 at 04:20
  • I have tried to do the first one and it doesn't give me an error like before but it still doesn't show the specific post. (I updated the question to show the profile page ) – Migdotcom Jul 10 '17 at 16:24
  • but your queryset is wrong? did you changed it to the one i gave? Post.objects.filter(author__username= 'name of author') place the username of the author you want to give and the view will be Detailview, the listview will show all the posts from all the authors and Detailview will take a authors id and show only his post – Exprator Jul 10 '17 at 16:28
  • Sorry I was being too vague. The query set worked perfectly but I ran into two problems. It only works when in the admin I set the author as the user I choose. Second problem was that I have to manually put the authors name. Once again thanks for the help I really appreciate it – Migdotcom Jul 10 '17 at 16:53
0

The other answers are good, but I think, since you have related_name ='user_posts' defined, the better way to retrieve the posts that correspond to a User instance would be doing it based on the related_name field:

 user_posts = <the user>.blog_posts.all()
Nifled
  • 412
  • 3
  • 15
  • Sorry I am pretty new to django could you expand more? I like the idea of using related post but still don't know where or how. – Migdotcom Jul 10 '17 at 13:10
  • So since you want to show a Profile page, what you should really do is create a UserDetailView, and your object would be a user object, so in your template you can reference your User's fields, for example: `

    {{ user.first_name }}

    `, `

    {{ user.last_name }}

    `, `{% for post in user.blog_posts.all %}`, and things like that
    – Nifled Jul 10 '17 at 16:26