UnboundLocalError: cannot access local variable 'u_form' where it is not associated with a value

@login_required def profile(request): if request.user.groups.filter(name='Student').exists(): try: profile = request.user.studentprofile except StudentProfile.DoesNotExist: profile = StudentProfile(user=request.user)

    if request.method == 'POST':    
        u_form = UserUpdateForm(request.POST,instance=request.user)
        p_form = ProfileUpdateForm(request.POST, 
                                instance=profile)

        if p_form.is_valid():
            p_form.save()
            messages.success(request, "Your information has been updated successfully.")
            return redirect('user-profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=profile)

elif request.user.groups.filter(name='Employee').exists():
    try:
        profile = request.user.employeeprofile
    except EmployeeProfile.DoesNotExist:
        profile = EmployeeProfile(user=request.user)
        
    if request.method == 'POST':
        p_form = EmployeeProfileUpdateForm(request.POST, 
                                instance=profile)
    
        if p_form.is_valid():
            p_form.save()

            messages.success(request, "Your information has been updated successfully.")
            return redirect('user-profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = EmployeeProfileUpdateForm(instance=profile)

context = {
    "u_form": u_form,
    "p_form": p_form,
}
return render(request, 'accounts/profile.html', context)


I've been expecting for the django application to display the user  profile but the error code is preventing that from actualizing
Вернуться на верх