django表单刷新时重新提交 [英] django form resubmitted upon refresh

查看:1310
本文介绍了django表单刷新时重新提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我在模板中的表单:我在第一次提交表单,然后刷新表单后,它将被重新提交,我不想要。

 < form action =method =POST> {%csrf_token%} 
{{form.as_p}}
< input type =submitvalue =Shout!/>
< / form>

我该如何解决?



这是我的观点:

  def index(request):
shouts = Shout.objects.all()

如果request.method ==POST:
form = GuestBookForm(request.POST)
如果form.is_valid():
cd = form.cleaned_data
shout = Shout(author = cd ['author'],message = cd ['message'])
shout.save()
form = GuestBookForm()
else:
form = GuestBookForm()

return render_to_response('guestbook / index.html',{'shouts':shouts,
'form':form},
context_instance = RequestContext (请求))


解决方案

我的猜测是这是一个你的看法中的问题



成功提交和处理Web表单后,您需要使用返回值 HttpResponseRedirect ,即使你只是重定向到同一个视图。否则,某些浏览器(我确定FireFox会这样做)最终会提交两次表单。



这是一个如何处理这个例子...



  def some_view(request):
if request.method ==POST:
form = some_form request.POST)
如果form.is_valid():
#处理
#保存模型等
返回HttpResponseRedirect(/ some / url /)
return render_to_response(normal / template.html,{form:form},context_instance = RequestContext(request))

鉴于您最近添加的视图...

  def index(request):
喊= Shout.objects.all()

如果request.method ==POST:
form = GuestBookForm(request.POST)
如果form.is_valid():
cd = form.cleaned_data
shout = Shout(author = cd ['author'],message = cd ['message'])
shout.save()

#重定向到此视图,假设它位于某些应用程序
返回HttpResponseRedirect(reverse(some_app.views.index))
else:
form = GuestBookForm()

return render_to_response('guestbook / index.html',{'shouts':shouts,
'form':form},
context_instance = RequestContext(request))

这将使用 reverse 重定向到同一个视图(如果这是你想要做的)


After I submit the form for the first time and then refresh the form it gets resubmitted and and I don't want that.

Here's my form in template :

<form action = "" method = "POST"> {% csrf_token %}
    {{ form.as_p }}
    <input type = "submit" value = "Shout!"/>
</form>

How can I fix this ?

Here's my views:

def index(request):
    shouts = Shout.objects.all()

    if request.method == "POST":
        form = GuestBookForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            shout = Shout(author = cd['author'], message = cd['message'])
            shout.save()
            form = GuestBookForm()
    else:
        form = GuestBookForm()

    return render_to_response('guestbook/index.html', {'shouts' : shouts,
                                             'form' : form },
                              context_instance = RequestContext(request))

解决方案

My guess is that this is a problem in your view.

After successful submission and processing of a web form, you need to use a return HttpResponseRedirect, even if you are only redirecting to the same view. Otherwise, certain browsers (I'm pretty sure FireFox does this) will end up submitting the form twice.

Here's an example of how to handle this...

def some_view(request):
  if request.method == "POST":
    form = some_form(request.POST)
    if form.is_valid():
      # do processing
      # save model, etc.
      return HttpResponseRedirect("/some/url/")
  return render_to_response("normal/template.html", {"form":form}, context_instance=RequestContext(request))

Given your recently added view above...

def index(request):
    shouts = Shout.objects.all()

    if request.method == "POST":
        form = GuestBookForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            shout = Shout(author = cd['author'], message = cd['message'])
            shout.save()

            # Redirect to THIS view, assuming it lives in 'some app'
            return HttpResponseRedirect(reverse("some_app.views.index"))
    else:
        form = GuestBookForm()

    return render_to_response('guestbook/index.html', {'shouts' : shouts,
                                         'form' : form },
                          context_instance = RequestContext(request))

That will use reverse to redirect to this same view (if thats what you are trying to do)

这篇关于django表单刷新时重新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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