Django unable to delete/clear data on form

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

i have a edit_profile view at my django application that also checks if the pgp key the users saves to his profile is in RSA format, Anyways if i add a profile avatar for the very first time it works like a charm, if i want to clear or delete it, im always jumping onto the execpt block and the user avatar remains unchanged. Well i dont see a clear reason why at the point can maybe smb give me a hint here:

views.py

def edit_profile(request):
if request.method == 'POST':
    form = UserForm(request.POST, request.FILES, instance=request.user)
    try:
        pubpgp = request.POST.get('pubpgp')
        if not pubpgp or PGPKey.from_blob(pubpgp.rstrip("\r\n"))[0].key_algorithm == PubKeyAlgorithm.RSAEncryptOrSign:
            if form.is_valid():
               form.save()
               messages.success(request, "Profile has been updated successfully.")
               return redirect(reverse('home'))
            else:
              print(form.errors)
              return render(request, 'app_Accounts/edit_profile.html', {'form': form})
        else:
            messages.error(request, "Uuups, something went wrong, please try again.")
            return render(request, 'app_Accounts/edit_profile.html', {'form': form})
    except Exception as e:
        print(e.args)
        messages.error(request, "PGP-Key is wrong formated.")
        return render(request, 'app_Accounts/edit_profile.html', {'form': form})
else:
    form = UserForm(instance=request.user)
    args = {'form': form}
    return render(request, 'app_Accounts/edit_profile.html', args)

forms.py

class UserForm(forms.ModelForm):

    class Meta:
        model = User
        fields = (
            'avatar',
            'bio',
            'pubpgp'
        )
    captcha = CaptchaField()

    field_order = ['avatar', 'bio', 'pubpgp']

    def
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

 

Try to detect forms error and exception error

and is_valid is required to save the form

try this

def edit_profile(request):
    if request.method == 'POST':
        form = UserForm(request.POST, request.FILES, instance=request.user)
        try:
            pubpgp = request.POST.get('pubpgp')
            if not pubpgp or PGPKey.from_blob(pubpgp.rstrip("\r\n"))[0].key_algorithm == PubKeyAlgorithm.RSAEncryptOrSign:
               if form.is_valid():
                   form.save()
                    messages.success(request, "Profile has been updated successfully.")
                   return redirect(reverse('home'))
              else:
                  print(form.errors)
                  return render(request, 'app_Accounts/edit_profile.html', {'form': form})
            else:
                messages.error(request, "Uuups, something went wrong, please try again.")
                return render(request, 'app_Accounts/edit_profile.html', {'form': form})
        except Exception as e:
            print(e.args)
            messages.error(request, "PGP-Key is wrong formated.")
            return render(request, 'app_Accounts/edit_profile.html', {'form': form})
    else:
        form = UserForm(instance=request.user)
        args = {'form': form}
        return render(request, 'app_Accounts/edit_profile.html', args)

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.