Passing **kwargs to Django Form

user_details is passed to __init__, so is not defined outside of it. That’s why you can’t access it when you’re instatiating that CharField object. Set initial in __init__ itself, after you’ve popped it from kwargs, for instance:

class PersonalInfoForm(forms.Form):

    username = forms.CharField(required=True)
    email = forms.EmailField(required=True)

    def __init__(self, *args, **kwargs):
        user_details = kwargs.pop('user_details', None)
        super(PersonalInfoForm, self).__init__(*args, **kwargs)
        if user_details:
            self.fields['username'].initial = user_details[0]['username']

When you get a chance, consider reading up on scopes in python.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)