需要根据电子邮件搜索用户,然后停用其个人资料

时间:2019-06-09 06:31:33

标签: django django-models django-forms django-views django-2.2

我正在尝试创建一个搜索表单,如果该帐户是正确的帐户,则管理员可以在其中搜索用户,然后停用其个人资料。

尝试基于函数的视图,然后尝试基于类的视图。它在基于功能的视图中显示配置文件,但不更新它。并且在基于类的视图中甚至不会显示配置文件。

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})

1 个答案:

答案 0 :(得分:0)

简单查询。

user=User.objects.get(email="user@email.com")
user.activate=false
user.save()