我試圖創建一個簡單的表單,它以用戶輸入的名稱和電子郵件爲例。 但我提交後,它沒有得到保存在數據庫 manage.py dumpdata顯示[]即空行。 請幫忙!無法將表單數據保存在數據庫中 - Django
models.py
class EmpD(models.Model):
name = models.CharField(max_length=100)
email=models.CharField(max_length=100)
forms.py
class SubmitF(forms.ModelForm):
name = forms.CharField()
email= forms.CharField()
class Meta:
model = EmpD
fileds = '_all_'
views.py
def submit_view(request):
template_name = 'emp/home.html'
redirect_url = 'emp/su.html'
form = SubmitF()
if request.method == 'POST':
form = SubmitF(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(redirect_url)
else:
return render(request, template_name, {'form': form})
def index(request):
return render(request, 'emp/home.html')
包含HTML形式:
<form action='' method="post" >
{% csrf_token %}
{{ form }}
<table border="0" cellpadding="5" cellspacing="0">
<tr> <td style="width: 50%">
<label for="Name"><b>First name *</b></label><br />
<input name="Name" type="text" maxlength="50" style="width: 260px" />
</td> <td style="width: 50%">
</td> </tr> <tr> <td colspan="2">
<label for="Email_Address"><b>Email *</b></label><br />
<input name="Email_Address" type="text" maxlength="100" style="width: 535px"
/>
</td> </tr> <tr> <td colspan="2">
<input type="submit" value="Submit Data" />
</td> </tr>
</table>
</form>
應用urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.submit_view, name='submit_view')]
請,固定的縮進views.py代碼,並顯示您的網址 –
('的Fileds = '_all_'' - >'欄=' _all_'') – PRMoureu
@PRMoureu其實它是'fields ='__all __'':) –