“返回到该页面可能导致您需要重复已进行的任何行动” - Django的(“Returning t

2019-06-26 18:49发布

我有我的网站,创建数据库的条目的形式。 所以每次当我刷新页面我第一次得到这个消息时间:

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>

Answer 1:

此消息是从浏览器; 它会显示每次尝试刷新已显示为POST请求的结果页面。

这对你的代码没有影响,该浏览器将显示所有在您尝试刷新页面网站相同的消息(命中例如F5),其显示为一前一后请求的结果。

为了防止这种情况的发生,确保所有POST请求重定向到完成时有不同的看法; 而不是渲染模板本身。



Answer 2:

只要你的重定向页面当前页面插入后,它会清除所有的价值和避免增加重复的记录!

例:

protected void btnAdd_Click(object sender, EventArgs e)
{
 //your code 
 Response.Redirect("Currentpage.aspx",true);

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


Answer 3:

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

header("Location: #");


文章来源: “Returning to that page might cause any action you took to be repeated” - Django