2016-01-23 32 views
1

我有這樣的代碼編輯帖子,它幾乎可以工作,但是當我點擊最終編輯按鈕時,它將我帶到404頁面,爲什麼? (精刪除作品)

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() 

它幾乎工作,只有貼可以編輯和我跟着刪除同樣的方法,它工作正常用戶。我只是不確定它爲什麼贏了,不適合編輯表格。當用戶去編輯url時,它會顯示與帖子表單相同的編輯表單。但是當用戶點擊編輯按鈕時,它會帶我到http://127.0.0.1:8000/edit/這不存在,但編輯網址是http://127.0.0.1:8000/post/xx/edit/。當我寫這個問題時,我意識到我應該在我的edit.html <form id="post_form" method="post" action="/edit/" enctype="multipart/form-data"> 中更改操作?添加後的動作是「/ add_post /」

+0

請添加'urls.py'。 – Leistungsabfall

+0

您需要使用表單操作屬性顯示模板。 –

回答

3

表單中的操作是表單數據將被提交到的url。你有,

<form id="post_form" method="post" action="/edit/" enctype="multipart/form-data"> 

所以表單將被提交到網址/edit/

提交表單到/post/xx/edit/,你可以做

<form id="post_form" method="post" action="/post/{{ post.id }}/edit/" enctype="multipart/form-data"> 

如果編輯URL相同崗位的絕對URL,你可以做

<form id="post_form" method="post" action="{{ post.get_absolute_url }}" enctype="multipart/form-data"> 

如果您提交的目前的網址,你可以完全忽略這個動作。

<form id="post_form" method="post" enctype="multipart/form-data"> 
+0

謝謝,但我遇到了這個問題,我做了一個新的線程。你介意在這個高峯期? http://stackoverflow.com/questions/35171044/wrote-editing-feature-that-wont-work-very-confused-what-action-should-in-my-fo –

相關問題