as_view()(用于基于类的视图)可以在装饰器中实现吗?

发布于 2024-11-30 02:26:24 字数 481 浏览 0 评论 0原文

Django 基于类的视图的 as_view() 语法一直困扰着我。基本上,我厌倦了必须为每个我想使用的基于类的视图执行 my_view = MyView.as_view() (或者将 as_view() 放在我使用基于 c 的视图的每个 url conf 的每一行中)。我认为类装饰器将是实现此目的的一种更简洁的方法,并且我正在尝试找出如何实现这一点,对于我自己的代码并在 django 片段上共享,以防其他人感兴趣。所以我的问题是:是否有一种合理的方法来实现“as_view”装饰器,以便以下代码可以正常工作?

@as_view(my_view)
class MyView(View):
   pass

这基本上相当于:

class MyView(View):
   pass
my_view = MyView.as_view()

谢谢,这也帮助我学习Python的高级功能(即装饰器)。

Django's as_view() syntax for Class-based views has been bugging me. Basically, I am tired of having to do a my_view = MyView.as_view() for every class-based view that I want to use (or putting as_view() in every line of every url conf I use c-based views in). I think a class decorator would be a cleaner way to implement this, and I am trying to figure out how to acheive that, for my own code and to share on django snippets, in case others are interested. So my question is: Is there a reasonable way to implement an "as_view" decorator so that the following code would be functional?

@as_view(my_view)
class MyView(View):
   pass

which would basically be equivalent to:

class MyView(View):
   pass
my_view = MyView.as_view()

Thanks, this is also helping me learn the advanced features (i.e. decorators) of python.

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2024-12-07 02:26:24

我想到的一种方法是让类装饰器只是setattr某个对象,该对象将包含您想要定义的所有as_views。然后你可以在你的 urls.py 中使用它

import re

class Views(object):
    """Empty object for holding as_view method"""
views = Views()

def convert_name(name):
    """convert CapWords into cap_words"""
    return name[0].lower() + re.sub(r'([A-Z])', lambda m:"_" + m.group(0).lower(), name[1:])

def as_view(views):
    """Adds decorated class-based views' as_view methods to views container"""
    def decorator(cls):
        name = convert_name(cls.__name__)
        setattr(views, name, cls.as_view)
        return cls
    return decorator

,然后在你的views.py中你可以像这样使用它:

from as_view_decorator import views, as_view

@as_view(views)
class MyView(View):
    ....

..并且 MyView.as_view 将存储在 views 上对象为 views.my_view

您还可以在views.py中执行类似的操作:

from as_view_decorator impor as_view
import sys; mod = sys.modules[__name__]

@as_view(mod)
class MyView(View):
    ...

这会将my_view设置为views模块上的属性。然后,您可以在 urls.py 中:

from app.views import my_view

urlpatterns = patterns('',    
    url(r'^

我认为 my_view = MyView.as_view 非常简单且可读,所以坦率地说,我自己更喜欢这样做。

, my_view()), )

我认为 my_view = MyView.as_view 非常简单且可读,所以坦率地说,我自己更喜欢这样做。

One approach that comes to mind is to have the class decorator just setattr some object which will contain all the as_views you want defined. You could then use that in your urls.py

import re

class Views(object):
    """Empty object for holding as_view method"""
views = Views()

def convert_name(name):
    """convert CapWords into cap_words"""
    return name[0].lower() + re.sub(r'([A-Z])', lambda m:"_" + m.group(0).lower(), name[1:])

def as_view(views):
    """Adds decorated class-based views' as_view methods to views container"""
    def decorator(cls):
        name = convert_name(cls.__name__)
        setattr(views, name, cls.as_view)
        return cls
    return decorator

And then in your views.py you would use it like this:

from as_view_decorator import views, as_view

@as_view(views)
class MyView(View):
    ....

..and MyView.as_view would be stored on the views object as views.my_view.

You could also do something like this in your views.py:

from as_view_decorator impor as_view
import sys; mod = sys.modules[__name__]

@as_view(mod)
class MyView(View):
    ...

Which would set my_view as an attribute on your views module. You could then in your urls.py:

from app.views import my_view

urlpatterns = patterns('',    
    url(r'^

I think doing my_view = MyView.as_view is pretty simple and readable, so I'd frankly prefer that myself.

, my_view()), )

I think doing my_view = MyView.as_view is pretty simple and readable, so I'd frankly prefer that myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文