[Fixed]-How to update a variable within a View.py

1👍

if request.method == "POST":
    form = OnBoardingProgress( request.POST )
        if form.is_valid():
            ....
            // Can I increment the code here? //
            ....
            obj = form.save(commit=False)                
            obj.user = current_user
            obj.save()

            user_obj = UserProfile.objects.get(user=request.user)
            user_obj.onboarding_step = user_obj.onboarding_step + 1
            user_obj.save()

            return render(request, "nextpage.html", {'form': form })

or you can make autoincrement field also.

0👍

Get the UserProfile object for the current user and then increment the value of the attribute of onboarding_step.

Try this:

if request.method == "POST":
    form = OnBoardingProgress(request.POST)
    current_user = request.user
    if form.is_valid():  
        user_profile = UserProfile.objects.filter(user=current_user)[0] # get the user profile object for the current user
        user_profile.onboarding_step += 1 # increment the value  
        user_profile.save() # save the object                 
        obj = form.save(commit=False)
        obj.user = current_user
        obj.save()

        return render(request, "nextpage.html", {'form': form })

Leave a comment