0

视图.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)

我正在使用模型表单,同时单击保存按钮页面可以导航,但是我输入的任何数据都没有保存在数据库中。

谢谢

4

2 回答 2

0

当您分配电话的用户时,您有此行:

phone.user=request.user()

但它应该没有括号,因为request.user is not callable. 像这样更正它:

phone.user=request.user

试一试。你的代码看起来不错。

此外,表单可能无法验证,因为您必须blank=True像这样在模型中添加用户的字段声明:

user = models.ForeignKey(User, null=True, blank=True)

希望能帮助到你。

于 2013-05-06T13:21:48.373 回答
0

单击保存按钮页面时可以导航,但是我输入的任何数据都没有保存在数据库中。

这是因为您没有 else 条件来匹配您的phoneForm.is_valid(),并且由于您的重定向是 external 的一部分,因此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})

在您的模板中,确保显示表单中的任何错误。有关如何自定义模板中的错误输出,请参阅手册的这一部分。

于 2013-05-06T13:22:29.463 回答