2014-10-10 48 views
0

我试图添加使用django &'POST'方法提交的学生的详细信息。 提交时,我将详细信息保存在我的'py'文件中。之后,我想重定向到主页。 早些时候我用在django中提交表单后重定向到带有参数的主页

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

但在主页的每个耳目一新,它将插入数据表。 然后我尝试使用“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)) 

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> 
+0

化妆名同一主题的另一篇文章作为unigue在Django模型 – 2014-10-10 09:05:53

+0

=真当你得到你的答案时,将其标记为已验证。 – ancho 2014-10-10 11:08:53

+0

但是不同学生的名字,班级等可以相同 – user123 2014-10-11 04:54:20

回答

0

尝试检查,如果请求后首次在add.py

如。

msg = "" 

    if request.method == "POST": 
     stud = student(name=request.POST['studname']) 
     stud.save() 

     msg = "saved" 

    return render_to_response("home.html",{ "edit":0,"msg":msg}, context_instance=RequestContext(request)) 
+0

我检查'request.method',但在每次刷新'主页'页面提交后,我得到POST方法。所以它是插入数据的时间与我为RPG链接节省 – user123 2014-10-10 08:58:34

4

你应该看看Post/Redirect/Get

基本上,

  1. 确保,你有一个处理后
  2. 然后将用户重定向到一个特定的页面POST方法。
  3. 然后有一个GET请求来提供页面。

这里是你能想到:

使用Django的redirect

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") 

下面是对PRG pattern

+0

+1的时间一样多 – Shuo 2014-10-10 10:45:21