"返回该页面可能会导致您重复采取任何行动" - Django

Vor*_*Vor 11 django django-models django-database

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

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?
Run Code Online (Sandbox Code Playgroud)

显然,我不希望在我的数据库中多次使用相同的信息.

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

#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>
Run Code Online (Sandbox Code Playgroud)

Bur*_*lid 15

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

它与您的代码无关,浏览器将在您尝试刷新页面(F5例如命中)的所有网站上显示相同的消息,该页面是由于之前的POST请求而显示的.

要防止这种情况发生,请确保所有POST请求在完成时重定向到其他视图; 而不是自己渲染模板.

  • 非常感谢你.但有什么方法可以避免它吗? (2认同)
  • 是的,POST请求应该在完成后发出重定向,这将防止显示此错误. (2认同)
  • 另一种防止它的方法:/sf/ask/3195948381/ (2认同)