S
S
s_katala2018-06-10 18:34:25
Django
s_katala, 2018-06-10 18:34:25

How to fix matching query does not exist?

Exception Value:	
UserProfile matching query does not exist.

def PostList(request):
  all_posts = Post.objects.order_by('-timestamp').all()
  paginator = Paginator(all_posts, 3)
  page = request.GET.get('page', 1)
  try:
    posts = paginator.page(page)
  except PageNotAnInteger:
    raise Http404
  except EmptyPage:
    posts = paginator.page(paginator.num_pages)
  post_votes = {}
  if request.user.is_authenticated:
    for post in posts:
      try:
        vote = Vote.objects.get(vote_object_type=post.get_content_type(), vote_object_id=post.id, user=UserProfile.objects.get(user=request.user))
        post_votes[post.id] = vote.value
      except Vote.DoesNotExist:
        pass

  return render(request, 'posts/post_list.html', {'posts': posts, 'post_votes': post_votes})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Web Dentist, 2018-06-10
@s_katala

Um what the...
OK.
Pack UserProfile.objects.get(user=request.user)
in a separate try

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question