“返回该页面可能会导致您重复执行的任何操作” - Django

Posted

技术标签:

【中文标题】“返回该页面可能会导致您重复执行的任何操作” - Django【英文标题】:"Returning to that page might cause any action you took to be repeated" - Django 【发布时间】:2012-08-02 10:13:05 【问题描述】:

我的网站上有一个表格,可以在数据库中创建一个条目。因此,每次刷新页面时,我都会首先收到此消息:

The page that you're looking for used information that you entered.
Returning to that page might cause any action you took to be repeated.
Do you want to continue?

显然我不想在我的数据库中多次使用相同的信息。

以防万一:这是我的代码(我知道有很多废话需要删除):

#views.py
@login_required
def subject(request,username, subject_name):
    subject_id = Subjects.objects.filter(user = request.user).get(name=subject_name)
    #Upload form
    if request.method == "POST":
        if "upload-b" in request.POST:
            form = ContentForm(request.POST, request.FILES, instance=subject_id)       
            if form.is_valid(): # need to add some clean functions
                 up_f = FileDescription.objects.get_or_create(subject=subject_id,
                                                  subject_name=subject_name,
                                                  file_type=request.POST['file_type'],
                                                  file_uploaded_by = username,
                                                  file_name=request.POST['file_name'],
                                                  file_description=request.POST['file_description'],
                                                  image = request.FILES['image'],
                                                  )
form = ContentForm()

#Show uploaded files with respect to clicked session (Homework, Class , Random ... )
homework_files = Homework.homework.filter(subject_name__exact=subject_name,
                                         file_uploaded_by__exact=username)
class_files = ClassPapers.classpapers.filter(subject_name__exact=subject_name)
random_files = RandomPapers.randompapers.filter(subject_name__exact=subject_name,
                                           file_uploaded_by__exact=username)




return render_to_response('subject_content.html', 'form':form,
                                                   'subject_name': subject_name,
                                                   'class_files': class_files,
                                                   'homework_files': homework_files,
                                                   'class_files': class_files,
                                                   'random_files': random_files,
                                                   ,
                           context_instance=RequestContext(request))


#forms.py:
class ContentForm(forms.ModelForm):
    file_name =forms.CharField(max_length=255, widget=forms.TextInput(attrs='size':20))
    file_description = forms.CharField(widget=forms.Textarea(attrs='rows':4, 'cols':25))
    class Meta:
        model = FileDescription 
        exclude = ('subject', 'subject_name', 'file_uploaded_by')


#template
    <div id="sbj-creation-frm">
        <h3>Upload File</h3>
        <form action="." method="post" enctype="multipart/form-data">% csrf_token %
             form.as_p 
            <input type="submit" value="submit" name="upload-b" class="btn-create" />
        </form>
    </div>

【问题讨论】:

【参考方案1】:

此消息来自浏览器;它会在您尝试刷新作为 POST 请求结果显示的页面时显示。

它与您的代码无关,浏览器将在您尝试刷新页面的所有网站上显示相同的消息(例如按 F5) POST 请求。

为防止这种情况发生,请确保所有 POST 请求在完成后重定向到不同的视图;而不是自己渲染模板。

【讨论】:

非常感谢。但是有什么办法可以避免呢? 是的,POST 请求在完成后应该发出重定向,这样可以防止显示此错误。 另一种预防方法:***.com/questions/45656405/… 可以,如果是ASP.net MVC,使用return RedirectToAction("Index");参考[link]***.com/a/25673124/900284【参考方案2】:

重定向到为我工作的同一页面:

header("Location: #");

【讨论】:

【参考方案3】:

插入后只需将您的页面重定向到当前页面 ,它将清除所有值并避免添加重复记录!

示例:

protected void btnAdd_Click(object sender, EventArgs e)

 //your code 
 Response.Redirect("Currentpage.aspx",true);

 //or 
 Response.Redirect(Request.Url.AbsoluteUri);

【讨论】:

我收到错误Use of undefined constant Response - assumed 'Response' 因为问题是针对 Django 的,而这个答案是针对...是 .net 吗?

以上是关于“返回该页面可能会导致您重复执行的任何操作” - Django的主要内容,如果未能解决你的问题,请参考以下文章