“返回到该页面可能会导致您重复执行任何操作” - Django [英] "Returning to that page might cause any action you took to be repeated" - Django

查看:149
本文介绍了“返回到该页面可能会导致您重复执行任何操作” - Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个表单,它在数据库中创建一个条目。所以每当我刷新页面时,我先得到这个消息:

 你正在寻找的页面使用了你的信息进入。 
返回到该页面可能会导致您重复执行任何操作。
你想继续吗?

显然,我不想在我的数据库中有不止一次有相同的信息。



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

 #views.py 
@login_required
def subject(request,username,subject_name):
subject_id = Subjects.objects.filter(user = request.user ).get(name = subject_name)
#Upload form
如果request.method ==POST:
如果request.POST中的upload-b:
form = ContentForm(request.POST,request.FILES,instance = subject_id)
如果form.is_valid():#需要添加一些干净的函数
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()

#相对于点击会话(家庭作业,类,随机...)上传的文件
homework_files =家庭作业。 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 =用户名)




返回选择render_to_response('subject_conte nt.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:
类ContentForm(forms.ModelForm):
file_name = forms.CharField(max_length = 255,widget = forms.TextInput(attrs = {'size':20}))
file_description = forms.CharField = forms.Textarea(attrs = {'rows':4,'cols':25}))
class Meta:
model = FileDescription
exclude =('subject','subject_name file_uploaded_by)


#template
将DIV ID = SBJ创造-FRM >
将H3>上传文件< / H3>
< form action =。 method =postenctype =multipart / form-data> {%csrf_token%}
{{form.as_p}}
< input type =submitvalue =submitname = 上传-b 类= BTN-创建/>
< / form>
< / div>


解决方案

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



这对你的代码没有影响,该浏览器将显示在您尝试刷新由于之前的POST请求而显示的页面(例如,点击 F5 )的所有网站上的相同消息。



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


I have a form on my website, that creates an entry in database. So every time when I refresh a page I got this message first:

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?

Obviously I don't want have the same information more than once in my database.

just in case: this is my code (I know there is a lot of crap that needs to be deleted):

#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>

解决方案

This message is from the browser; and it will display anytime you try to refresh a page that was displayed as the result of a POST request.

It has no bearing on your code, the browser will display the same message on all websites where you try to refresh the page (hit F5 for example) which was displayed as a result of a previous POST request.

To prevent this from happening, make sure all POST requests redirect to a different view upon completion; and not render templates themselves.

这篇关于“返回到该页面可能会导致您重复执行任何操作” - Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆