0

I want to auto populate my update form in django with the existing data, but instance=request.user and instance=request.user.profile seems not to be auto populating the form

views.py

def UserProfile(request, username):
    # user_profiles = Profile.objects.all()
    user = get_object_or_404(User, username=username)
    profile = Profile.objects.get(user=user)
    url_name = resolve(request.path).url_name

    context = {
        'profile':profile,
        'url_name':url_name,
    }

    return render(request, 'userauths/profile.html', context)





@login_required
def profile_update(request):
    info = Announcements.objects.filter(active=True)
    categories = Category.objects.all()
    user = request.user
    profile = get_object_or_404(Profile, user=user)

    if request.method == "POST":
        u_form = UserUpdateForm(request.POST, instance=request)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile.user)
        if u_form.is_valid() and p_form.is_valid():

            u_form.save()
            p_form.save()
           
            messages.success(request, f'Acount Updated Successfully!')
            return redirect('profile', profile.user.username)
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)
    
    context = {
        'u_form': u_form,
        'p_form': p_form,
    }
    return render(request, 'userauths/profile_update.html', context)

1 Answer 1

3

I finally figured it out, what you need to is make sure to pass in request.user and also request.user.profile in the else: that shows that the forms is a GET request

CODE SAMPLE


def profile_update(request):
    profile = Profile.objects.get(user=request.user)
    if request.method == "POST":
        form = ProfileForm(request.POST, request.FILES, instance=profile)
        if form.is_valid():
            new_form = form.save(commit=False)
            new_form.user = request.user
            new_form.save()
            messages.success(request, "Profile Updated Successfully.")
            return redirect("core:dashboard")
    else:
        form = ProfileForm(instance=profile)

    context = {
        "form": form,
        "profile": profile,
    }

    return render(request, "userauths/profile-edit.html", context)

Firstly Get the instance of whatever model you are trying to update, then in the line where you request.POST the data that would be passed into the input fields, also add instance=model_instance_you_are_trying_to_update in my case it was the profile so i did instance=Profile, let's say yours is a blog post then

  1. Get the blog post
post = Posts.object.get(id=id)
postUpdateForm(request.POST, instance=post) # the instance=post would the existing fields 
Sign up to request clarification or add additional context in comments.

2 Comments

Any chance you could add your final code? Im having the same problem? (or did you updatae your post with your solution?)
I have updated my answer with the code that can help you achieve that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.