本文介绍了写的编辑功能将无法正常工作,很困惑我应该采取什么行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我写的代码用户可以在他们想要的时候编辑这个帖子。我可以通过删除成功完成,但是在用户点击最终编辑按钮时进行编辑,它不会被编辑。我有, http://127.0.0.1:8000/ post / hello / 为你的帖子。现在编辑页面 http://127.0.0.1:8000/post/edit/hello/ 。最后,当用户点击确定编辑时,应该让我回到 http://127.0.0.1:8000/post/hello/ 与编辑版本。但是它不会被编辑。

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

for edit.html

<form id="post_form" method="post" action="/post/{{ post.slug }}/" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form|crispy }}


推荐答案

表单需要提交到编辑页面,以便可以处理和保存数据。

The form needs to submit to the edit page, so that the data can be processed and saved.

这篇关于写的编辑功能将无法正常工作,很困惑我应该采取什么行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:37