Skip to content
Advertisement

Why is Django forms fields value not rendering in html template for function based update view?

The issue is when i try to update my profile, i do not see the exising value i do not actually know what is wrong with the views..

views.py

def profile_update(request):
    info = Announcements.objects.filter(active=True)
    categories = Category.objects.all()

    profile = get_object_or_404(Profile, user=request.user)
    
    Profile.objects.get_or_create(user=request.user)
    if request.method == "POST":
        u_form = UserUpdateForm(request.POST, instance=profile)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=profile)
        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(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
    
    context = {
        'u_form': u_form,
        'p_form': p_form,
        'info': info,
        'categories': categories
    }

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

Advertisement

Answer

in your form = UpdateFormExample(request.POST, instance=request.user) this fixed the issue

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement