saving . . . saved How to populate a form from an instance in model when the form is using Form instead of ModelForm? has been deleted. How to populate a form from an instance in model when the form is using Form instead of ModelForm? has been hidden .
How to populate a form from an instance in model when the form is using Form instead of ModelForm?
Title
Question
I am making a Django project where users can sign up, sign in, view their profile and change it if they want to.

I want to know how to implement the functionality of a user changing their profile?

Here is my user form in forms.py
```
class CustomerForm(forms.Form):
    username = forms.CharField(label='Desired Username', max_length=150, validators=[validate_username])
    first_name  = forms.CharField(label='First Name', max_length=150)
    last_name = forms.CharField(label='Last Name', max_length=150)
    email = forms.EmailField(label='Your Email', validators=[validate_email])
    password1 = forms.CharField(label='Enter Password', widget=forms.PasswordInput, min_length=8)
    password2 = forms.CharField(label='Retype Password', widget=forms.PasswordInput, min_length=8)

    def clean(self):
        cleaned_data = super().clean()
        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")

        if password1 != password2:
            raise ValidationError(
                "Passwords do not match"
            )
```

Here is my sign-up method in views.py
```
"""Function for sign up."""
def sign_up(request):
    if request.method == 'POST':
        form = CustomerForm(request. POST)
        if form.is_valid():
            try:
                user = User.objects.create_user(
                    request.POST['username'],
                    request.POST['email'], request.POST['password1']
                )
                user.first_name = request.POST['first_name']
                user.last_name = request.POST['last_name']
                user.save()
                request.session['normal_username'] = request.POST['username']
                login(request, user)
                return redirect('../book/')
            except Exception:
                return HttpResponse("Something went wrong. Please try again.")
        else:
            context = {'form': form}
            return render(request, 'sign_up.html', context)

    context = {'form': CustomerForm()}
    return render(request, 'sign_up.html', context)
```
<a href="https://docs.djangoproject.com/en/1.8/_modules/django/contrib/auth/forms/" target="" title="UserCreationForm">I cannot user UserCreationForm because it is depreciated.</a>

The main problem that I am facing is that I cannot populate the form using 'instance' parameter since I am using Form instead of ModelForm. Is there a way to populate the form?
```
"""Function for editing profile."""
def edit_profile(request):
    profile = User.objects.get(username=request.session['normal_username'])

    if request.method == 'POST':
        form = CustomerForm(request.POST, instance=profile)
        if form.is_valid():
            form.save()
            return redirect('../book/')
        else:
            context = {'form': form}
            return render(request, 'sign_up.html', context)
    context = {'form': CustomerForm(instance=profile)}
    return render(request, 'edit.html', context)


Python-Django Creating-Forms-in-Django 11-12 min 10-20 sec 12-10-22, 1:06 a.m. Anshul_Gupta

Answers:

Log-in to answer to this question.