2012-02-17 207 views
2

我有一個Django模型/視圖/表單在模板中正確呈現,但它沒有提交輸入到數據庫的數據。任何幫助,將不勝感激!Django表單沒有提交

#models.py 

from django.db import models 
from django.forms import ModelForm 

class UserRegistration(models.Model): 
    user_first = models.CharField(max_length=50) 
    user_last = models.CharField(max_length=50) 
    user_email = models.EmailField() 
    #user_fantasyhost = models.CharField(max_length=50) 

    def __unicode__(self): 
     return u'%s %s %s' % (self.user_first, self.user_last, self.user_email) 

class RegForm(ModelForm): 
    class Meta: 
     model = UserRegistration 

#views.py 

from django.shortcuts import render_to_response 
from django.shortcuts import render 
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect 
from acme.dc_django.models import UserRegistration 
from acme.dc_django.models import RegForm 

def regPage(request, id=None): 
    form = RegForm(request.POST or None, 
         instance=id and UserRegistration.objects.get(id=id)) 

    if request.method == 'POST' and form.is_valid(): 
     form.save() 
     return HttpResponseRedirect('/league_setup/') 
    user_info = UserRegistration.objects.all() 
    context = { 
     'form':form, 
     'user_info' :user_info, 
    } 

    return render(request, 'regpage.html', context) 

#repage.html 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<HTML lang="en"> 
<head> 
    <title>User Registration</title> 
</head> 
<body> 

    <form method="POST" action="/league/"> 
     {% csrf_token %} 
     <table>{{ form }}</table> 
     <input type="submit" value="Create Account" 
    </form><br /><br /> 

</body> 
</HTML> 

感謝你的幫助,

DP

+1

您還沒有關閉您的表單提交的標籤,但這可能只是一個錯字,你的瀏覽器可能可以處理它。這種問題通常比我們更容易調試。視圖出錯了?在視圖中放置一些打印語句以查明。例如,什麼是'form.is_valid()'的值。 – Alasdair 2012-02-17 21:23:23

+1

請你詳細說明什麼是不工作?我發現'但它沒有提交輸入到數據庫的數據'混淆 – dm03514 2012-02-17 21:52:25

+0

你有錯誤嗎? – Jurudocs 2012-02-17 22:20:42

回答

4

我想你的代碼。您的問題是,您的html 表單標記動作屬性設置爲「/ league /」。

除非reqPage網址實際上是「/ league /」,否則它將不起作用。當我改變action="/league/"action=""這樣:

<HTML lang="en"> 
<head> 
    <title>User Registration</title> 
</head> 
<body> 

    <form method="POST" action=""> 
     {% csrf_token %} 
     <table>{{ form }}</table> 
     <input type="submit" value="Create Account" /> 
    </form><br /><br /> 

</body> 
</HTML> 

形式工作:

In [3]: UserRegistration.objects.all() 
Out[3]: [<UserRegistration: aoeu oeu [email protected]>] 
+0

,我做了同樣的事情,得到了同樣的結果!感謝您的幫助! DP – dpbklyn 2012-02-20 18:11:08