-1

我必须将 views.py 文件中的“is_staff”选项更改为禁用 Django-admin 页面,但我无法解决以下问题。每当我尝试编写“user.is_staff”时,听起来没有任何选项可以选择它(is_staff),而 is_active 存在。这是进口的问题吗?

以下是我要导入的内容:

from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.models import User
from django.http import HttpResponseForbidden, HttpResponse
from django.shortcuts import get_object_or_404
from django.views.generic.list_detail import object_detail
from django.views.generic.simple import direct_to_template
from django.utils.translation import ugettext as _
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

以下代码我在views.py中编写:

def user_change_status(request, id):
user = User.objects.get(pk=id)
    if user.is_staff:
       user.is_staff = False
    else:
       user.is_active = True
return HttpResponse('')

整个场景是我有一个模板,它显示了所有用户的列表以及他/她的 is_staff 选项(真/假)。当超级用户选择任何用户 is_staff 选项时我想要什么,它会改变并且页面将重定向到同一页面上。

编辑后: views.py中定义了两个方法:

def user_change_status(request, id):
user = User.objects.get(pk=id)
if user.is_active:
    user.is_staff = False
else:
    user.is_staff = True
user.save()
value2 = user.is_staff
return HttpResponse(value2)

另一个是`

def user_block(request, id):
user = User.objects.get(pk=id)
if user.is_active:
    user.is_active = False
else:
    user.is_active = True
user.save()
value1 = user.is_active
return HttpResponse('value1')    

我想更改 is_staff 值和 is_active 值。方法 user_change_status 不起作用,而 user_block 起作用。

4

2 回答 2

5

Python 是一种动态语言。特别是,它不是 Java 或 C++。通常,IDE 在动态语言的自动完成方面做得很差。

注意到您的 IDE 提供或不提供什么作为自动完成选项是完全错误的。有时它会做对,有时它不会。有时它会提供根本不是对象成员的选项。

使用文档,而不是您的 IDE。

于 2012-05-13T12:25:57.830 回答
1

user_change_status删除is_staff活动用户,但启用is_staff非活动用户,这是你想要做的吗?实际上不是要切换 is_staff 值吗?我问,因为user_block切换is_active值。

如果是这样,你应该更换

if user.is_active:
    user.is_staff = False

if user.is_staff:
    user.is_staff = False
于 2012-05-13T11:16:05.033 回答