目前,我正在从事数学家庭作业项目。有三种类型的用户。教授,学生和管理员。以下片段将拒绝用户根据用户的类型访问网址。修改后,您必须将此方法装饰器添加到服务于http请求的请求方法中。
def deny_a_thing(function):
def wrapper(request, *args, **kw):
try:
test = User.objects.get(username=request.user) # This will get the username
except ObjectDoesNotExist:
raise Http404 # return 404, if the user doesn't exist. You can change it to anything
if not request.user.is_authenticated(): # If the user is not authenticated, return to the rootm. You can also change this.
return HttpResponseRedirect("/")
try:
if test.groups.filter()[0].name != "professors": # Add your test here. E.g: If user saved A, don't go B.
raise Http404
except IndexError:
raise Http404
else:
return function(request, *args, **kw)
return wrapper
以下内容添加到您的方法
@deny_a_thing # This will deny the user from doing the thing you didn't want the user to do.
def some_page_request(request):
return HttpResponse("OMG")
检查这个link一个简单的装饰
这有什么不好使用会话? – 2014-10-17 17:18:53
你看过[django的表单向导](https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/)吗? – dgel 2014-10-17 17:25:47