Hi, happy new year!
I'm trying to put the Django Girls comments functionality onto my blog, which I've copied from another tutorial elsewhere. It's going pretty well, but I want to be able to put the post title into the comment form page (it's a separate page in this tutorial, I'd rather it was on the same page as the blog post but that's a bit tricky for me at this point).
This is as far as I've got:
The add_comment_to_post view:
def add_comment_to_post(request, pk):
post = get_object_or_404(Post, pk=pk)
def post_name(self, obj):
return obj.post.title
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect('/' + post.slug)
else:
form = CommentForm()
return render(request, 'blog/add_comment_to_post.html', {'form': form})
And here's the template:
{% extends 'base.html' %}
{% block content %}
<h1>New comment on {{ post_name }}</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Send</button>
</form>
{% endblock %}
Am I close?