From cc59254ba462216ec14607720e5ee4922a02924c Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 25 Dec 2020 12:24:09 +0100 Subject: [PATCH] Added clean_username function to validate form. --- django_lostplaces/lostplaces/forms.py | 8 ++++++++ django_lostplaces/lostplaces/views/explorer_views.py | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django_lostplaces/lostplaces/forms.py b/django_lostplaces/lostplaces/forms.py index 9629ff7..4a590e1 100644 --- a/django_lostplaces/lostplaces/forms.py +++ b/django_lostplaces/lostplaces/forms.py @@ -48,6 +48,14 @@ class ExplorerUserChangeForm(UserChangeForm): self.fields['username'].required = False self.fields['username'].help_text = None self.fields['username'].widget.attrs['disabled'] = 'disabled' + + def clean_username(self): + # As shown in the above answer. + instance = getattr(self, 'instance', None) + if instance: + return instance.username + else: + return self.cleaned_data.get('username', None) class ExplorerChangeForm(forms.ModelForm): class Meta: diff --git a/django_lostplaces/lostplaces/views/explorer_views.py b/django_lostplaces/lostplaces/views/explorer_views.py index 6773f07..ccc10ff 100644 --- a/django_lostplaces/lostplaces/views/explorer_views.py +++ b/django_lostplaces/lostplaces/views/explorer_views.py @@ -60,7 +60,6 @@ class ExplorerProfileUpdateView(IsAuthenticatedMixin, View): explorer_change_form = ExplorerChangeForm(request.POST, instance=request.user.explorer) if explorer_change_form.is_valid() and explorer_user_change_form.is_valid(): - print(explorer_user_change_form) explorer_user_change_form.save() explorer_change_form.save()