views.py
def add_phone(request):
phoneForm = PhoneForm()
if request.method=='POST':
phoneForm = PhoneForm(request.POST)
if phoneForm.is_valid():
phone=phoneForm.save(commit==False)
phone.user=request.user()
phone.save()
return redirect('/member/contact-list/')
return render_to_response('incident/add_phone.html',
{
'about_menu': True,
'PhoneForm' :phoneForm
},
context_instance=RequestContext(request))
template.py
<form action="/member/add-phone/" method="POST"> {% csrf_token %}
<table width="100%" cellpadding="0" cellspacing="0" id="phone">
<tr>
<td colspan="2"><h1 align="left">Call - default telephone numbers</h1></td>
</tr>
<tr>
<td colspan="2">Set the phone numbers that people should use if they <br />
need back-up in dealing with an incident.
</td>
</tr>
<tr>
<td>Person or area</td>
<td>Phone number</td>
</tr>
<tr>
<td>{{PhoneForm.number1}}</td>
<td>{{PhoneForm.number1}}</td>
</tr>
<tr>
<td>{{PhoneForm.name2}}</td>
<td>{{PhoneForm.number2}}</td>
</tr>
<tr>
<td>{{PhoneForm.name3}}</td>
<td>{{PhoneForm.number3}}</td>
</tr>
<tr>
<td>Emergency</td><td>Phone number</td>
</tr>
<tr>
<td>{{PhoneForm.emergency}}</td>
<td>{{PhoneForm.emergency_number}}</td>
</tr>
<tr><td colspan="2" align="right"> <p style=margin-top:2cm;>{% include "buttons/save.html" %}</p></td></tr>
</table></form>
forms.py
class PhoneForm(forms.ModelForm):
class Meta:
model = Phone_info
models.py
class Phone_info(models.Model):
user = models.ForeignKey(User, null=True)
name1 = models.CharField('Name', max_length=100, null=True, blank=True)
number1 = models.CharField('Number',max_length=20, null=True, blank=True)
name2 = models.CharField('Name', max_length=100, null=True, blank=True)
number2 = models.CharField('Number', max_length=20, null=True, blank=True)
name3 = models.CharField('Name', max_length=100, null=True, blank=True)
number3 = models.CharField('Number',max_length=20, null=True, blank=True)
emergency = models.CharField('Emergency', max_length=100, null=True, blank=True)
emergency_number = models.CharField('Emergency Number',max_length=20, null=True, blank=True)
我正在使用模型表单,而点击保存按钮页面会导航,但我输入的数据不会保存在数据库中。
由于
答案 0 :(得分:0)
分配手机用户时,您有此行:
phone.user=request.user()
但它应该没有括号,因为request.user is not callable
。像这样纠正:
phone.user=request.user
并尝试一下。你的代码似乎很好。
此外,表单可能无法验证,因为您必须在模型中将blank=True
添加到用户的字段声明中,如下所示:
user = models.ForeignKey(User, null=True, blank=True)
希望它有所帮助。
答案 1 :(得分:0)
单击保存按钮页面时,可以导航但是我的数据是什么 输入不保存在数据库中。
这是因为您没有其他条件来匹配phoneForm.is_valid()
,因为您的重定向是外部if
的一部分,即使表单未验证,页面也会重定向。
试试这个版本:
from django.shortcuts import render
def add_phone(request):
""" Responds to /member/contact-list/ and
adds a phone to the database """
phoneForm = PhoneForm()
if request.method=='POST':
phoneForm = PhoneForm(request.POST)
if phoneForm.is_valid():
phone=phoneForm.save(commit=False)
phone.user=request.user
phone.save()
return redirect('/member/contact-list/')
else:
# Form didn't validate
return render(request,
'incident/add_phone.html',
{'PhoneForm': phoneForm,
'about_menu': True})
return render(request,
'incident/add_phone.html',
{'about_menu': True, 'PhoneForm': phoneForm})
在模板中,确保显示表单中的任何错误。请参阅手册的this section,了解如何在模板中自定义错误输出。