簡體   English   中英

第二次重新填充Django表單

[英]Repopulating a Django form for the second time

我正在使用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)

Confirm-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 %}

表格

從Django導入表單從.models導入博客

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

我可能會誤解您的代碼,但是為什么要在elif 'no'子句中重新創建表單? 實際上,這不是代碼重復已經完成的事情,並且您不能只是執行以下操作:

elif 'no' in request.POST:
    pass

當您單擊“刪除”時,是否會出現一個新的警報窗口? 如果是這樣,您可以將其關閉,而不是將“否”按鈕設為提交按鈕。 如果要打開新的HTML表單,則可能類似於window.close()

提交表單時,將僅提交該<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM