简体   繁体   English

用装饰器覆盖Django视图

[英]Overriding Django views with decorators

I have a situation that requires redirecting users who are already logged in away from the login page to another page. 我遇到一种情况,需要将已经登录的用户从登录页面重定向到另一个页面。 I have seen mention that this can be accomplished with decorators which makes sense, but I am fairly new to using them. 我已经看到提到可以使用有意义的装饰器来完成此操作,但是我对使用它们是相当陌生的。 However, I am using the django login and a third party view (from django-registration). 但是,我正在使用django登录名和第三方视图(来自django-registration)。 I do not want to change any of the code in django.contrib.auth or django-registration. 我不想更改django.contrib.auth或django-registration中的任何代码。 How can I apply a decorator to a view that is not to be modified in order to get the desired behavior. 如何将装饰器应用于未经修改的视图,以获取所需的行为。

Thanks in advance! 提前致谢!

UPDATE: I discovered that I mistakenly associated the login function with the registration module. 更新:我发现我错误地将登录功能与注册模块相关联。 django-registration has nothing to do with this issue. django-registration与这个问题无关。 However, I still need to be able to override default login() behavior. 但是,我仍然需要能够覆盖默认的login()行为。 Any thoughts? 有什么想法吗?

Three more ways to do it, though you'll need to use your own urlconf for these: 还有另外三种方法,尽管您需要为此使用自己的urlconf:

  1. Add the decorator to the view directly in the urlconf: 直接在urlconf中将装饰器添加到视图中:

     ... (regexp, decorator(view)), ... 

    You need to import the view and the decorator into the urlconf though, which is why I don't like this one. 您需要将视图和装饰器导入urlconf中,这就是为什么我不喜欢这一视图。 I prefer to have as few imports in my urls.py's as possible. 我希望urls.py中的导入尽可能少。

  2. Import the view into an <app>/views.py and add the decorator there: 将视图导入<app>/views.py并在其中添加装饰器:

     import view view = decorator(view) 

    Pretty much like Vinay's method though more explicit since you need an urlconf for it. 尽管您需要一个urlconf,但是它更像Vinay的方法,但更明确。

  3. Wrap the view in a new view: 将视图换成新视图:

     import view @decorator def wrapperview(request, *args, **kwargs): ... other stuff ... return view(request, *args, **kwargs) 

    The last one is very handy when you need to change generic views. 当您需要更改通用视图时,最后一个非常方便。 This is what I often end up doing anyway. 无论如何,这就是我经常要做的。

Whenever you use an urlconf, order of patterns matter, so you might need to shuffle around on which pattern gets called first. 每当使用urlconf时,模式的顺序都很重要,因此您可能需要重新安排首先调用哪种模式。

If you have the decorator function and you know which view in django-registration you want to decorate, you could just do 如果您具有装饰器功能,并且知道要在django-registration中装饰哪个视图,则可以

registration.view_func = decorator_func(registration.view_func)

where registration is the module in django-registration which contains the view function you want to decorate, view_func is the view function you want to decorate, and decorator_func is the decorator. 其中registration是django-registration中的模块,其中包含要装饰的视图函数, view_func 要装饰的视图函数,而decorator_func是装饰器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM