2014-10-10 52 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