视图.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))
模板.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>
表格.py
class PhoneForm(forms.ModelForm):
class Meta:
model = Phone_info
模型.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)
我正在使用模型表单,同时单击保存按钮页面可以导航,但是我输入的任何数据都没有保存在数据库中。
谢谢