def comment_add(request): if request.method == "POST": comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.author = ... comment.snippet = ... comment.save()
def comment_add(request): if request.method == "POST": comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.author = request.user comment.snippet = request.Snippet.id comment.save() return redirect(f'/snippet_page/{request.Snippet.id}') return redirect('home')
def comment_add(request): if request.method == "POST": snippet_id = request.POST["snippet_id"] comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.author = request.user snippet = Snippet.objects.get(id=snippet_id) comment.snippet = snippet comment.save() return redirect(f'/snippet/{snippet_id}') raise Http404
def snippet_page(request, id): snippet = Snippet.objects.get(id=id) comments = Comment.objects.filter(snippet_id=id) form_snippet = SnippetForm(instance=snippet) form_comment = CommentForm() context = get_base_context(request, "Стр сниппета") context["form_snippet"] = form_snippet context["snippet"] = snippet return render(request, 'pages/snippet_page.html', context)
def snippet(request, snippet_id): context = get_base_context(request, 'Страница снипета') try: item = Snippet.objects.get(id=snippet_id) comments = item.comment_set.all() except Snippet.DoesNotExist: raise Http404 context['comments'] = comments context['comment_form'] = CommentForm() context['snippet'] = item return render(request, 'pages/snippet.html', context)
def snippet(request, snippet_id): context = {'pagename': 'Страница сниппета'} try: snippet = Snippet.objects.get(id=snippet_id) # comments = snippet.comment_set.all() except Snippet.DoesNotExist: raise Http404 context["snippet"] = snippet form = CommentForm() context["comment_form"] = form return render(request, 'pages/snippet.html', context)