2016-08-11 89 views
0

我正在用Django製作一個基本的博客平臺,我在編輯頁面添加了一個可以保存新編輯版本的編輯頁面,並且還添加了一個刪除選項。當單擊刪除按鈕時,會呈現一個新模板,要求確認或取消刪除。當我點擊「是」時,該帖子會根據需要自行刪除,當我點擊「否」時,它會返回到「編輯」頁面。問題是,當它返回到'編輯'頁面時,表單不再被填充,所有的字段都是空的。有沒有什麼辦法可以配置'no'選項以返回到所有填寫數據的'edit'頁面?請告知如果問題不清楚。謝謝。第二次重新生成Django表單

views.py

def edit_post(request, slug): 

    post=get_object_or_404(Blog, slug=slug) 
    form = BlogForm(request.POST or None, instance=post) 

    context = { 
     'title': post.title, 
     'post': post, 
     'form': form, 
     } 

    if 'save' in request.POST: 
     if form.is_valid(): 
      post=form.save(commit=False) 
      post.save() 
      return HttpResponseRedirect(post.get_absolute_url()) 

    elif 'delete' in request.POST: 
     return render(request, 'Blog/confirm-deletion.html',delete_context) 

    #Now delete template is shown, and view takes 'yes' or 'no' option 

    elif 'yes' in request.POST: 
     post.delete() 
     messages.success(request, 'Post was successfully deleted') 
     return HttpResponseRedirect(reverse('post_list')) 

    elif 'no' in request.POST: 
     form = BlogForm(request.POST, instance=post) 
     context = { 
      'title': post.title, 
      'post': post, 
      'form': form, 
      } 
     return render(request, 'Blog/edit_post.html', context) 
     #PROBLEM HERE: CAN'T GET *NO* OPTION TO RETURN TO FILLED OUT FORM PAGE 

return render(request, 'Blog/edit_post.html', context) 

確認,deletion.html

{% extends 'Blog/base.html' %} 

{% block content %} 
<form method='POST' action = '' enctype='multipart/form-data'> {% csrf_token%} 
    <p>Are you sure you want to delete {{title}}?</p> 
    <input type = 'submit' name = 'yes' value = 'Yes'/> 
    <input type = 'submit' name = 'no' value = 'No'/> 
</form> 

{% endblock %} 

forms.py

從Django的進口形式 從.models導入博客

class BlogForm(forms.ModelForm): 
    class Meta: 
     model = Blog 
     fields = [ 
     'title', 
     'category', 
     'content', 
     'draft' 
     ] 
+0

當您單擊「否」,在'

'標籤的所有字段將提交給服務器。如果您想要提交博客表單域,那麼您需要將表單放在同一個標​​記中。 – Alasdair

+0

@Alasdair所以重新提交到'編輯'頁面的信息來自'是/否'的形式,而不是博客形式,這就是爲什麼字段留空? –

+0

@Alisdair,只是試了一下,完美的作品。如果你想留下你的評論作爲答案,我會選擇它作爲最有幫助的。非常感謝您的幫助。 –

回答

0

當您提交表單時,只會提交<form>標記中的字段。例如,假設你有兩個頁面上的表單標籤:

<form method='POST' action = '' enctype='multipart/form-data'> 
    {% csrf_token%} 
    {{ form }} 
    <input type='submit' name='submit' value='Submit' /> 
</form> 

<form method='POST' action = '' enctype='multipart/form-data'> {% csrf_token%} 
    <p>Are you sure you want to delete {{title}}?</p> 
    <input type = 'submit' name = 'yes' value = 'Yes'/> 
    <input type = 'submit' name = 'no' value = 'No'/> 
</form> 

當您單擊「否」按鈕,從後表單中的字段將不會被提交,因爲它們以不同的形式標記。

提交表單數據,當您點擊「不」,你需要在同一個標​​籤的形式:

<form method='POST' action = '' enctype='multipart/form-data'> 
    {% csrf_token%} 
    {{ form }} 
    <input type='submit' name='submit' value='Submit' /> 
    <p>Are you sure you want to delete {{title}}?</p> 
    <input type = 'submit' name = 'yes' value = 'Yes'/> 
    <input type = 'submit' name = 'no' value = 'No'/> 
</form> 
0

我可能會誤解你的代碼,但你爲什麼要重新創建elif 'no'條款中的表單?其實,是不是重複什麼已經完成的代碼,而你就不能這樣做:

elif 'no' in request.POST: 
    pass 
+0

不幸的是,如果「否」沒有被點擊/選擇,那麼用戶將只保留在'confirm-deletion.html'頁面上,直到選擇是或否。因此,如果我在'否'上使用_pass_,我將不會選擇返回到原始'編輯'頁面,除非我使用我的瀏覽器後退按鈕(這對我來說很sl so,所以我寧願避免) –

+0

@MattCraig爲什麼呢?它不會執行「return render(request,'Blog/edit_post.html',context)」嗎? 我看到的另一種解決方案是清除request.POST並使用它來調用edit_post函數。 –

0

當你點擊「刪除」就一個新的警告窗口來呢?如果是這樣,而不是使'否'按鈕成爲提交按鈕,您可以讓它關閉警報框。如果它打開一個新的HTML表單,可能類似window.close()

+0

現在我只是渲染一個新的模板/ html頁面,所以當'delete'被選中時,服務器會加載一個新的頁面。不過,這可能是一個好主意,我可能會在後面用JavaScript來設置它(除非在Django/Python中有一個簡潔的方法)? –

相關問題