Python 从views.py到Django表单的数据

Python 从views.py到Django表单的数据,python,django,forms,Python,Django,Forms,我是Django的新手,我已经学到了一些关于它的基本知识。我的问题是,我从django模型获取数据,但我无法将其传递/显示到表单。我想要一个带有1个电话id选择器和3个文本输入的forms.py,以便将数据插入所需的电话 My models.py: class Phone(models.Model): user = models.ForeignKey(User) num_calls = models.CharField(max_length=20, null=True, blan

我是Django的新手,我已经学到了一些关于它的基本知识。我的问题是,我从django模型获取数据,但我无法将其传递/显示到表单。我想要一个带有1个电话id选择器和3个文本输入的forms.py,以便将数据插入所需的电话

My models.py:

class Phone(models.Model):
    user = models.ForeignKey(User)
    num_calls = models.CharField(max_length=20, null=True, blank=True)
    time_btwn_calls = models.CharField(max_length=20, null=True, blank=True)
    psap = models.CharField(max_length=30, null=True, blank=True)
My forms.py:

from django import forms

class phoneForm(forms.Form):

    NumberOfCalls = forms.CharField(
        min_length = 1,
        widget=forms.TextInput({'class': 'form-control'})
        )

    TimeBetweenCalls = forms.CharField(
        widget=forms.TextInput({'class': 'form-control'})
        )

    PSAP = forms.CharField(
        min_length = 1,
        widget=forms.TextInput({'class': 'form-control'})
        )

    def __init__(self, *args, **kwargs):
        phone_choices = kwargs.pop('phone_choices')
        super(Send2tcu, self).__init__(*args, **kwargs)

        self.fields['phone'] = forms.MultipleChoiceField(
            required = True,
            widget = forms.Select({'class': 'form-control'}),
            choices = phone_choices
        )
我只是创建了一个包含3个文本输入和多个EchoiceField的表单,在这里我需要显示来自不同手机id的数据

My view.py:

def phone_config(request):
    phones = Phone.objects.filter(user_id = request.user.id).values_list('id', flat=True)

    if request.method == 'POST':
        form = phoneForm(request.POST, phone_choices=phones)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            phone_id = cleaned_data.get('phone')
            NumberOfCalls = cleaned_data.get('NumberOfCalls')
            TimeBetweenCalls = cleaned_data.get('TimeBetweenCalls')
            PSAP = cleaned_data.get('PSAP')
            Tcu.objects.filter(id=phone_id).update(
                num_calls = NumberOfCalls,
                time_btwn_calls = TimeBetweenCalls,
                psap = PSAP,
            )
            return redirect(reverse('gracias'))
    else:
        form = phoneForm(phone_choices=phones)
    return render(request, 'configurer/configurer.html', {'form': form})


def gracias_view(request):
    return render(request, 'configurer/gracias.html')
在我看来,首先,我得到了当前用户的所有电话号码。然后我检查方法是否为post,从表单中获取数据,并将不同的电话ID传递给表单。然后我检查表单是否有效,并创建对象电话。之后,将不同的参数分配给所选的电话号码并保存

我的代码有问题。我得到这个错误:

/configurer处的类型错误/

“int”对象不可编辑

返回呈现(请求'heroconfigurer/heroconfigurer.html',{'form': 表格})

models.py:

class Phone(models.Model):
        user = models.ForeignKey(User)
        num_calls = models.CharField(max_length=20, null=True, blank=True)
        time_btwn_calls = models.CharField(max_length=20, null=True, blank=True)
        psap = models.CharField(max_length=30, null=True, blank=True)
forms.py:

from django import forms

class phoneForm(forms.Form):

    NumberOfCalls = forms.CharField(
        min_length = 1,
        widget=forms.TextInput({'class': 'form-control'})
        )

    TimeBetweenCalls = forms.CharField(
        widget=forms.TextInput({'class': 'form-control'})
        )

    PSAP = forms.CharField(
        min_length = 1,
        widget=forms.TextInput({'class': 'form-control'})
        )

    def __init__(self, *args, **kwargs):
        phone_choices = kwargs.pop('phone_choices')
        super(Send2tcu, self).__init__(*args, **kwargs)

        self.fields['phone'] = forms.MultipleChoiceField(
            required = True,
            widget = forms.Select({'class': 'form-control'}),
            choices = phone_choices
        )
from django.forms.widgets import TextInput, Select

class PhoneViewForm(ModelForm):

    class Meta:
        model = Phone
        widgets = {'user': Select(attrs={'class': 'form-control'}),
                  'num_calls': TextInput(attrs={'class': 'form-control'}),
                  'time_btwn_calls': TextInput(attrs={'class': 'form-control'}), 
                  'psap': TextInput(attrs={'class': 'form-control'})
                  }
        fields = ['user', 
                  'num_calls', 
                  'time_btwn_calls', 
                  'psap'
                  ]

如果您在表单中操作模型对象,Django建议您使用ModelForm。您也可以用初始模型实例填写此表单。希望对您有所帮助。

您能包括堆栈跟踪吗?(您可能还想在某个时候了解
ModelForm
s)C:\Python27\lib\site packages\django-1.8.5-py2.7.egg\django\forms\widgets.py,位于render_options,第539行