Python Django表单:设置CharFields的初始值

时间:2014-11-16 20:16:49

标签: python django forms

我正在编写一个网页,作为更大的django项目的一部分,该项目允许现有用户更改其个人信息。问题是表单需要访问用户的现有信息。我想设置它,以便在页面加载时,每个CharField中都会​​显示现有信息,并且用户可以进行适当的更改并接受。页面加载,但charfield根本不显示。这就是我到目前为止所做的:

class EditPersonForm(forms.Form):

"""
Form that allows for editing an existing user's (of Person class) personal information.
Same structure as the RegistrationForm. 
The CharField's look the same as the registration page, but have the existing info already filled out.
"""

#the following line are all indented under the class definition, dont know why it's showing up weird here

def __init__(self, person):
    profile = person

    profile_default = {"username" : profile.user.username, "email" : profile.user.email, "first_name" : profile.user.first_name, \
        "last_name" : profile.user.last_name, "street_address" : profile.street_address, "apt_number" : profile.apt_number, \
        "city" : profile.city, "state" : profile.city, "zipcode" : profile.zipcode}

    username       = forms.CharField(label =('User Name'), max_length = constants.MAX_LEN_NAME, initial = profile_default.username)
    email          = forms.EmailField(label = ('Email Address'), initial = profile_default.email)
    first_name     = forms.CharField(label = ('First Name'), max_length = constants.MAX_LEN_NAME, initial = profile_default.first_name)
    last_name      = forms.CharField(label = ('Last Name'), max_length = constants.MAX_LEN_NAME, initial = profile_default.last_name)
    street_address = forms.CharField(label = ('Street Address'), max_length = constants.MAX_LEN_NAME, initial = profile_default.street_address)
    apt_number     = forms.CharField(label = ('Apartment Number'), required = False, max_length = constants.MAX_LEN_SHORTF, initial = profile_default.apt_number)
    city           = forms.CharField(label = ('City'), max_length = constants.MAX_LEN_NAME, initial = profile_default.city)
    state          = forms.ChoiceField(widget = forms.Select(),choices = constants.STATE_CHOICES, initial = profile_default.state)
    zipcode        = forms.CharField(label = ('Zipcode'), max_length = constants.MAX_LEN_SHORTF, initial = profile_default.zipcode)
    password       = forms.CharField(label = ('Password'), widget=forms.PasswordInput(render_value=False), max_length = constants.MAX_LEN_NAME)

    class Meta:
        """
        Class that allows the form to use the Person
        object model as a guideline.
        """
        model = Person
        exclude = ('user',)

2 个答案:

答案 0 :(得分:1)

对于__init__方法中的每个字段,您需要:

class EditPersonForm(forms.Form):

    def __init__(self, person, *args, **kwargs):
        super(EditPersonForm, self).__init__(*args, **kwargs)
        profile = person

        [...] #ommited code for simplification

        self.fields['username']= forms.CharField(
           label =('User Name'), max_length = constants.MAX_LEN_NAME, 
           initial = profile_default.username)

        [...] # do the same for rest of the fields

此外,由于您要在课程model中定义Meta,您认为是否使用了ModelForm

class EditPersonForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(EditPersonForm, self).__init__(*args, **kwargs) 
        [...]


    class Meta:
        model = Person
        exclude = ('user',)

希望这会有所帮助: - )

答案 1 :(得分:0)

没有必要这样做。字段应该在类级别定义,而不是在__init__方法中定义:执行此操作的唯一原因是在您希望以编程方式覆盖某些字段的某些方面的情况。将所有这些定义移到方法之外,并删除它们的initial属性。

相反,当您在视图中实例化表单时,将initial dict作为关键字参数传递:

form = EditPersonForm(initial=profile_default)