在Django中提交表单后,使用参数重定向到首页

use*_*123 1 python django python-2.7 django-1.4

我正在尝试添加使用django和“ POST”方法提交的学生详细信息。提交后,我将详细信息保存在“ py”文件中。之后,我想重定向到主页。我以前用过

return render_to_response("home.html",{ "edit":0,"msg":'saved'}, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

但是在每次刷新主页时,它将在表中插入数据。然后,我尝试使用“ HttpResponseRedirect”,但它不支持参数传递。我该如何解决?

add.py

stud = student(name=request.POST['studname'])
stud.save()
return render_to_response("home.html",{ "edit":0,"msg":'saved'}, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

student.html

<form id = "addForm" name = "addForm" action = "" method = 'POST' >
<table>
 <tr>
   <td>Name.</td>
   <td><input type="text" name="studname" id="studname"></td> 
 </tr>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)

Nag*_*tri 5

您应该查看Post / Redirect / Get

基本上,

  1. 确保您具有处理该帖子的POST方法
  2. 然后将用户重定向到特定页面。
  3. 然后,该请求具有GET请求以服务该页面。

您可以这样想:

使用Django的重定向

def submitting_student(request):
   if request.method == "POST":
        stud = student(name=request.POST['studname'])
        stud.save()
        return redirect("home.html", { "edit":0, "msg":'saved' })

   return render_to_response("student.html")
Run Code Online (Sandbox Code Playgroud)

这是关于PRG模式的同一主题的另一篇文章