Reputation: 99
Hello I wrote the code users to be able to edit the post when they want to. I could've done it successfully with delete, but for edit when the user clicks finalize edit button at the end, it won;t be edited.I have, http://127.0.0.1:8000/post/hello/
for hello post. Now for edit page http://127.0.0.1:8000/post/edit/hello/
.And lastly when user clicks finalize edit it should take me back to http://127.0.0.1:8000/post/hello/
with edited version. However it doesn't get edited.
views.py
class PostUpdateView(UpdateView):
model = Post
form_class = PostForm
template_name = 'main/edit.html'
def form_valid(self, form):
self.object = form.save(commit=False)
# Any manual settings go here
self.object.save()
return HttpResponseRedirect(self.object.get_absolute_url())
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
post = Post.objects.get(slug=kwargs['slug'])
if post.moderator == request.user:
return super(PostUpdateView, self).dispatch(request, *args, **kwargs)
else:
return http.HttpForbidden()
urls.py
url(r'^post/edit/(?P<slug>[\w|\-]+)/$', PostUpdateView.as_view(), name='post-edit'),
for edit.html
<form id="post_form" method="post" action="/post/{{ post.slug }}/" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
Upvotes: 0
Views: 28
Reputation: 599470
The form needs to submit to the edit page, so that the data can be processed and saved.
Upvotes: 1