Skip to content
This repository was archived by the owner on Jun 7, 2020. It is now read-only.

Commit 1a581c4

Browse files
committed
Made it possible to edit a comment.
1 parent 07dde68 commit 1a581c4

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

code/templates/wall/comment_edit.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends 'wall/base.html' %}
2+
3+
{% block content %}
4+
5+
<h1>Edit comment</h1>
6+
7+
<form method="POST" class="post-form">
8+
{% csrf_token %}
9+
{{ form.as_p }}
10+
<button type="submit" class="save btn btn-default">Edit Comment</button>
11+
</form>
12+
13+
{% endblock %}

code/templates/wall/post_details.html

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ <h2>{{ post.title }}</h2>
1414
<h3>{{ comment.title }}</h3>
1515
<p class='text'>{{ comment.text }}</p>
1616
<p class='fotter'><span class='date'>{{ comment.created_date }}</span></p>
17+
<a href="{% url 'comment_edit' comment.pk %}">Edit comment</a>
1718
</div>
1819
{% endfor %}
1920
<a href={% url 'comment_new' post_pk=post.pk %}>Add a comment</a>

code/wall/urls.py

+5
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
views.comment_write_new,
1212
name='comment_new'
1313
),
14+
url(
15+
r'^comment/(?P<pk>[0-9]+)/$',
16+
views.comment_edit,
17+
name='comment_edit'
18+
),
1419
)

code/wall/views.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from django.shortcuts import render
2-
from django.shortcuts import redirect
3-
1+
from django.shortcuts import render, redirect, get_object_or_404
42
from .forms import PostForm, CommentForm
5-
from .models import Post
3+
from .models import Post, Comment
64

75

86
def example_html_view(request):
@@ -60,7 +58,7 @@ def comment_write_new(request, post_pk):
6058
comment.save()
6159
return redirect('post_detail', pk=post_pk)
6260
else:
63-
form = PostForm()
61+
form = CommentForm()
6462

6563
return render(
6664
request,
@@ -69,3 +67,23 @@ def comment_write_new(request, post_pk):
6967
'post_pk': post_pk,
7068
}
7169
)
70+
71+
72+
def comment_edit(request, pk):
73+
74+
obj = get_object_or_404(Comment, pk=pk)
75+
76+
if request.method == "POST":
77+
form = CommentForm(request.POST, instance=obj)
78+
if form.is_valid():
79+
form.save()
80+
return redirect('post_detail', pk=obj.post.pk)
81+
else:
82+
form = CommentForm(instance=obj)
83+
84+
return render(
85+
request,
86+
'wall/comment_edit.html', {
87+
'form': form,
88+
}
89+
)

0 commit comments

Comments
 (0)