0
나는 사용자가 등급 세트와 함께 코멘트를 남길 수있는 모델 포스트를 가지고 있습니다. 내가 게시물 당 하나만 사용자 의견을 제한하고 싶습니다. 나는 문제가 내보기에 그 설정에django-forms : 로그인 한 사용자가 개별 게시물 당 하나의 댓글 만 제출하도록 허용
모델
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="comments")
user = models.ForeignKey(User, related_name="usernamee")
...
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
class Post(models.Model):
...
전망
def add_comment(request, slug):
post = get_object_or_404(Post, slug=slug)
# I tried wrapping all of the below in an "if" statement, something like
# if request.user.comment.exists(): to check if the user has already
# left a comment on this specific post, but I'm not sure of the right way to perform such a check here.
if request.method == 'POST':
form = CommentForm(request.POST or None)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.user = request.user
comment.email = request.user.email
comment.picture = request.user.profile.profile_image_url()
comment.save()
messages.success(request, "Thank you for leaving a review!")
return redirect('blog:post_detail', slug=post.slug)
else:
messages.error(request, "Something went wrong! We weren't able to process your review :(")
else:
form = CommentForm()
template = "blog/post/add_comment.html"
context = {
'form': form,
'post': post,
#'comment_count': comment_count
}
return render(request, template, context)
내가 필요 모두에서 전체 양식 코드를 래핑하는 것입니다 인상을 오전에 봉착 내 현재 로그인 한 사용자가 특정 게시물에 대한 주석을 이미 남겼는지 확인하는 일종의 검증 시스템의 add_comment 뷰 (보기의 주석 참조)
잠재적 인 해결책이 무엇인지 아는 사람이 있습니까? 아니면 제가 이걸 제대로하고 있다면?
감사합니다. 필요한 것은 'user_comments = post.comments.filter (user = request.user)'뿐입니다. 나는 사용자가 이미 리뷰를 남겼을 때 add_comment 양식 페이지에 액세스 할 수 있도록 if 문에 전체 양식 요청을 싸서 넣었다. 그것에 대해 생각할 때 너무 쉽지만, 내 인생에 대해 사용자가 주석을 달았는지 여부에 대한 정보를 얻는 방법을 알 수 없었습니다. 다시 한 번 감사드립니다! – dmandres
@dmandres 안녕하세요. – wencakisa