У меня есть модель комментария в моем django. Я просто использую контент для отправки с формой, но теперь я хочу отправить изображение также. Но он не работает, я пробую это разными способами, но он не работает.
views.py:
def post_detail(request, pk):
posts = get_object_or_404(post, pk=pk)
comments = Comment.objects.filter(post=posts, reply=None,).order_by('-timestamp')
#post=posts is the above post details posts its mean post for the exact corresponding posts
is_liked = False
if posts.likes.filter(id=request.user.id).exists():
is_liked = True
if request.method == 'POST':
comment_form = CommentForm(request.POST or None, request.FILES or None)
if comment_form.is_valid():
content = request.POST.get('content')
image = request.FILES['image']
reply_id = request.POST.get('comment_id')
comment_qs = None
if reply_id:
comment_qs = Comment.objects.get(id=reply_id)
comment = Comment.objects.create(post=posts, user=request.user, content=content, image=image, reply=comment_qs,)
comment.save()
notify.send(request.user, recipient=posts.author, actor=request.user, verb='comment in your post', target=posts, nf_type='comment_by_one_user')
else:
comment_form = CommentForm()
context = {'posts':posts, 'comments':comments, 'comment_form':comment_form, 'is_liked': is_liked, 'total_likes': posts.total_likes(),}
if request.is_ajax():
html = render_to_string('blog/comments.html', context, request=request)
return JsonResponse({'form': html})
return render(request, 'blog/post_detail.html', context)
и мой froms.py:
class CommentForm(forms.ModelForm):
content = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Text goes here', 'rows': '4', 'cols': '10'}))
class Meta:
model = Comment
fields = ('content', 'image',)
изображение не отправляется.