2015-12-15 55 views
2

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

我的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传递给表单。然后检查表单是否有效,然后创建对象Phone。之后,将不同的参数分配给选定的phone_id并保存。

我的代码有问题。我收到此错误:

TypeError at /configurer/

'int' object is not iterable

return render(request, 'heroconfigurer/heroconfigurer.html', {'form': form})

+2

您能否包含堆栈跟踪? (你也许想在某个时候阅读'ModelForm') – Sayse

+0

C:\ Python27 \ lib \ site-packages \ django-1.8.5-py2.7.egg \ django \ forms \ widgets.py in render_options, 539行 – picador

回答

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