语法错误/语法无效(forms.py,第6行)

时间:2018-10-01 17:29:44

标签: django django-forms

from django import forms


class UserProfileForm(forms.Form):
  first_name  = models.CharField(max_length=20, widget=forms.TextInput(attrs={'placeholder': 'First Name'})
  last_name   = models.CharField(max_length=20, widget=forms.TextInput(attrs={'placeholder': 'Last Name'})
  address     = models.CharField(max_length=75, widget=forms.TextInput(attrs={'placeholder': 'Address'})
  address2    = models.CharField(max_length=75, widget=forms.TextInput(attrs={'placeholder': 'Address 2'})
  postcode    = models.CharField(max_length=10, widget=forms.TextInput(attrs={'placeholder': 'Postcode'})
  phone       = models.IntegerField(max_length=15, widget=forms.TextInput(attrs={'placeholder': 'Mobile Number'})

SyntaxError位于/ 语法无效(forms.py,第6行)

这应该是男生的错误,但似乎无法解决。

1 个答案:

答案 0 :(得分:1)

在每一行中,打开两个括号:

  1. 一个为 field 形式;和
  2. 一个用于小部件。

但是在该行的末尾,您只需要关闭一个括号,因此在每一行中,您都需要添加一个额外的结束括号。

您还使用models.代替了forms.。这些是不同的,因为models.用于数据库列,而forms.以(HTML)格式封装项目。

from django import forms

class UserProfileForm(forms.Form):

  first_name  = forms.CharField(max_length=20, widget=forms.TextInput(attrs = {'placeholder': 'First Name'}))
  last_name   = forms.CharField(max_length=20, widget=forms.TextInput(attrs = {'placeholder': 'Last Name'}))
  address     = forms.CharField(max_length=75, widget=forms.TextInput(attrs = {'placeholder': 'Address'}))
  address2    = forms.CharField(max_length=75, widget=forms.TextInput(attrs = {'placeholder': 'Address 2'}))
  postcode    = forms.CharField(max_length=10, widget=forms.TextInput(attrs = {'placeholder': 'Postcode'}))
  phone       = forms.IntegerField(widget=forms.TextInput(attrs = {'placeholder': 'Mobile Number'}))