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.
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()
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
<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>
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')