B
B
bwylla2019-03-05 21:12:09
Django
bwylla, 2019-03-05 21:12:09

List of posts by a specific user?

How can I make an authorized user see in his profile a list of articles that he published on the site? Each article has the name of the author.

#models 
class Post(models.Model):
    title = models.CharField(max_length=100)
    text = models.TextField()
    author =  models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    date = models.DateTimeField(default=timezone.now)

#views
def post_list(request):
    posts = Post.objects.all()
    return render(request, 'home/post_list.html', {'posts':posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'home/post_detail.html', {'post':post})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-03-05
@bwylla

request.user.post_set.all()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question