从views.py到Django表单的数据

时间:2015-12-15 08:49:18

标签: python django forms

我是Django的新手,我已经学习了一些基本的东西。我的问题是,我从django模型获取数据,但我无法将其传递/显示到表单。我想有一个带有1个phone_id选择器和3个textInput的forms.py,以便将数据插入到所需的手机中。

我的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
        )

我只是创建一个包含3个textInputs和MultipleChoiceField的表单,我需要在其中显示来自不同的phone_id的数据。

我的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')

首先,在我的视图中,我获得了当前用户的所有phone_id。然后我检查方法是否发布,我从表单中获取数据,并将表单传递给不同的phone_ids.Then我检查表单是否有效,然后创建对象Phone。之后,将不同的参数分配给选定的phone_id并保存。

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

  

/ configurer /

中的TypeError      

' INT'对象不可迭代

     

返回渲染(请求,' heroconfigurer / heroconfigurer.html',{'表格':   形式})

1 个答案:

答案 0 :(得分:0)

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.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。您也可以使用初始模型实例填写此表单。希望有所帮助。