[Fixed]-Django model form for user profile won't pre-populate

1👍

Your main problem is you are only populating the form if the user hits the submit button, so when the view is requested initially, your form is empty.

from django.shortcuts import render, redirect

def editprofile(request):
    u_p = request.user.profile
    form = ProfileForm(request.POST or None, instance=u_p)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return redirect('/')
    return render(request,
                  'registration/editprofile.html', {'form': form})

Leave a comment