【发布时间】:2012-05-21 04:16:14
【问题描述】:
我必须将 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 起作用。
【问题讨论】:
-
“每当我尝试编写“user.is_staff”时,听起来没有任何选项可以选择它(is_staff),而 is_active 存在。”你有错误吗?或者你为什么假设它不存在?再说一遍......您的缩进不正确(用户 = ... 需要缩进)并且您在更改
is_staff之后和返回您的回复之前将您的用户保存在任何地方吗? -
我没有得到 is_staff 选项。在按 ctrl+space 键后有一些选项,例如 is_active,但在这里我没有得到那个
-
不确定您的 IDE 是做什么的。也许它只列出方法,而 is_staff 是用户模型上的一个字段 (docs.djangoproject.com/en/dev/topics/auth/#fields)。但现在我很困惑你的实际问题是什么;-)
-
完成!请查看“编辑后”部分
-
嘿@arie 等待您的回复 :)
标签: django django-models django-admin django-views