Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Django unable to delete/clear data on form

General Tech Bugs & Fixes

Max. 2000 characters
Replies

usr_profile.png

User

( 6 months 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
usr_profile.png

User

( 6 months 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)

what's your interest