def pet_detail(request, pk): pet = Pet.objects.get(pk=pk) # total_comments = Comment.objects.filter(pk=pk) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = Comment(comment=form.cleaned_data['comment']) comment.pet = pet comment.save() return redirect('pet-detail', pk) context = {'pet': pet, 'form': form} return render(request, 'pets/pet_detail.html', context) context = {'pet': pet, 'form': CommentForm()} return render(request, 'pets/pet_detail.html', context)
def show_pet_detail(req, pk): pet = Pet.objects.get(pk=pk) pet.likes_count = pet.like_set.count() if req.method == 'GET': return render(req, 'pets/pet_detail.html', get_detail_context(req, pet)) elif req.method == 'POST': form = CommentForm(req.POST) if form.is_valid(): comment = Comment(comment=form.cleaned_data['comment']) comment.pet = pet comment.user = req.user.userprofile # do not link to profile but user comment.save() pet.comment_set.add(comment) pet.save() # new_comment = Comment(pet=pet, comment=req.POST['comment']) # new_comment.save() # pet.comment_set.add(new_comment) return redirect('pet_details', pet.id)
def pet_details(req, pet_id): pet = Pet.objects.get(pk=pet_id) if req.method == 'GET': context = { 'pet': pet, 'comment_form': CommentForm() } return render(req, 'pets/pet_detail.html', context=context) else: form = CommentForm(req.POST) if form.is_valid(): comment = Comment(comment=form.cleaned_data['comment']) comment.pet = pet comment.save() return redirect('pet details', pet_id) context = { 'pet': pet, 'comment_form': form } return render(req, 'pets/pet_detail.html', context=context)