有人能告诉我如何为我的视图编写像@redirect_to_home
这样的登录修饰器,以便它将request.PATH
变量修改为像/
这样的新值,并将其应用于视图。修改请求路径的视图的自定义函数修饰器
我已经看到人们用装饰器做了相当复杂的事情:我还没有彻底弄懂它们。
感谢
有人能告诉我如何为我的视图编写像@redirect_to_home
这样的登录修饰器,以便它将request.PATH
变量修改为像/
这样的新值,并将其应用于视图。修改请求路径的视图的自定义函数修饰器
我已经看到人们用装饰器做了相当复杂的事情:我还没有彻底弄懂它们。
感谢
感谢彼得对他有用的例子。
def fake_requested_from_root(fn):
"""
Login decorator which when used on a view modifies the reqquest.path
to fool the template into thibking that the request is coming from the
root page
"""
def decorator(request, **kwargs):
request.path = reverse('home')
return fn(request, **kwargs)
return decorator
开始,最好的办法是了解从Django项目登录装饰(验证模块): http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/decorators.py#L33
如果你看一下“user_passes_test”功能,您将看到如何访问请求对象。
关于装饰很好的教程:http://www.ibm.com/developerworks/linux/library/l-cpdecor.html 对于有用装饰的一些例子看:http://wiki.python.org/moin/PythonDecoratorLibrary