0

I am trying to create a search form, Where admin can search users and then deactivate their profiles, if it is the right account.

tried function based views and then class based views. It shows the profile in function based views but doesn't update it. and in class based view it wouldn't even show the profile.

models.py

class User(AbstractBaseUser):
    objects = UserManager()
    email = models.EmailField(verbose_name='email address', max_length=255, unique=True,)
    type = models.CharField(max_length = 50, choices = type_choices)
    name = models.CharField(max_length = 100)
    department = models.CharField(max_length = 100, null = True, blank = True, choices = department_choices)
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False) # a superuser
    admin = models.BooleanField(default=False)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['type']

forms.py

class SearchForm(forms.Form):
    email = forms.EmailField(required=True)

views.py

@method_decorator(login_required, name='dispatch')
class adminDeleteProfileView(LoginRequiredMixin, View):
    def render(self, request):
        return render(request, 'admin/view_account.html', {'form': self.form})

    def form_valid(self, form):
        self.form = SearchForm(request.POST)
        print('im here', form.cleaned_data.get('email'))
        User.objects.filter(email = form.cleaned_data.get('email')).update(active = False)
        #print('Donot come here')

    def get(self, request):
        self.form = SearchForm()
        return self.render(request)

@login_required
def admin_deactivate_profile_view(request):
    error_text = ''
    if request.method == 'POST':
        print('here')
        user_email = request.POST.get('email')

        try:
            print('Deactivating',user_email, 'Account.')
            profile = User.objects.filter(email = user_email).first()

            if request.POST.get('delete'):
                User.objects.filter(email = user_email).update(active = False)
                messages.success(self.request, 'Profile Updated!')
        except Exception as e:
           print(e)
           messages.success(self.request, 'There was an error!')

    return render(request, "admin/delete_profile.html", {'profile':profile})
Rishabh
  • 31
  • 1
  • 5

1 Answers1

0

simple query .

user=User.objects.get(email="user@email.com")
user.activate=false
user.save()
biswa1991
  • 530
  • 3
  • 8
  • It doesn't work. I have already tried it. It just gives me another error. Method Not Allowed (POST): /administrator/find_profile Method Not Allowed: /administrator/find_profile – Rishabh Jun 09 '19 at 17:58
  • your view is wrong . you must have a post method to handle post request. and get method to handle get request. you did not define post method on view. – biswa1991 Jun 10 '19 at 09:43