如何在 Django 中编写请求过滤器/预处理器
我正在 Django 中编写一个应用程序,它在 url 中使用 [year]/[month]/[title-text]
来识别新闻项。为了管理这些项目,我定义了许多 URL,每个 URL 都以上述前缀开头。
urlpatterns = patterns('msite.views',
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/edit/$', 'edit'),
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/$', 'show'),
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/save$', 'save'),
)
我想知道 Django 中是否有一种机制,允许我预处理对视图 edit
、show
和 save
的给定请求。它可以解析参数,例如year=2010、month=11、slug='this-is-a-title'
并从中提取模型对象。
这样做的好处是,我可以将我的视图定义为“
def show(news_item):
'''does some stuff with the news item, doesn't have to care
about how to extract the item from request data'''
...
What
def show(year, month, slug):
'''extract the model instance manually inside this method'''
...
is the Django way to Solution this?” 或者以更通用的方式,是否有某种机制来实现请求过滤器/预处理器,例如在 JavaEE 和 Ruby on Rails 中?
I am writing an application in Django, which uses [year]/[month]/[title-text]
in the url to identitfy news items. To manage the items I have defined a number of urls, each starting with the above prefix.
urlpatterns = patterns('msite.views',
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/edit/
I was wondering, if there is a mechanism in Django, which allows me to preprocess a given request to the views edit
, show
and save
. It could parse the parameters e.g. year=2010, month=11, slug='this-is-a-title'
and extract a model object out of them.
The benefit would be, that I could define my views as
def show(news_item):
'''does some stuff with the news item, doesn't have to care
about how to extract the item from request data'''
...
instead of
def show(year, month, slug):
'''extract the model instance manually inside this method'''
...
What is the Django way of solving this?
Or in a more generic way, is there some mechanism to implement request filters / preprocessors such as in JavaEE and Ruby on Rails?
, 'edit'),
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/
I was wondering, if there is a mechanism in Django, which allows me to preprocess a given request to the views edit
, show
and save
. It could parse the parameters e.g. year=2010, month=11, slug='this-is-a-title'
and extract a model object out of them.
The benefit would be, that I could define my views as
instead of
What is the Django way of solving this?
Or in a more generic way, is there some mechanism to implement request filters / preprocessors such as in JavaEE and Ruby on Rails?
, 'show'),
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/save
I was wondering, if there is a mechanism in Django, which allows me to preprocess a given request to the views edit
, show
and save
. It could parse the parameters e.g. year=2010, month=11, slug='this-is-a-title'
and extract a model object out of them.
The benefit would be, that I could define my views as
instead of
What is the Django way of solving this?
Or in a more generic way, is there some mechanism to implement request filters / preprocessors such as in JavaEE and Ruby on Rails?
, 'save'),
)
I was wondering, if there is a mechanism in Django, which allows me to preprocess a given request to the views edit
, show
and save
. It could parse the parameters e.g. year=2010, month=11, slug='this-is-a-title'
and extract a model object out of them.
The benefit would be, that I could define my views as
instead of
What is the Django way of solving this?
Or in a more generic way, is there some mechanism to implement request filters / preprocessors such as in JavaEE and Ruby on Rails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要基于日期的通用视图 和 创建/更新/删除通用视图也许?
You need date based generic views and create/update/delete generic views maybe?
做到这一点的一种方法是编写一个自定义装饰器。我在我的一个项目中对此进行了测试,并且它有效。
首先,自定义装饰器。除了函数之外,这个函数还必须接受其他参数,因此我们声明另一个装饰器来实现这一点。
现在是实际的装饰器:
除了它正在装饰的函数之外,该装饰器还需要两个附加参数。它们分别是要为其准备和注入实例的模型类以及要用于准备实例的属性的名称。在这种情况下,装饰器使用属性从数据库中
获取
实例。现在,“通用”视图使用
show
函数。另外:
为了使其正常工作,URL 配置参数必须包含与属性相同的关键字。当然,您可以通过调整装饰器来自定义它。
正确更新
作为@rebus指出您还需要研究 Django 的通用视图。
One way of doing this is to write a custom decorator. I tested this in one of my projects and it worked.
First, a custom decorator. This one will have to accept other arguments beside the function, so we declare another decorator to make it so.
Now the actual decorator:
This decorator expects two additional arguments besides the function it is decorating. These are respectively the model class for which the instance is to be prepared and injected and the names of the attributes to be used to prepare the instance. In this case the decorator uses the attributes to
get
the instance from the database.And now, a "generic" view making use of a
show
function.And another:
In order for this to work the URL configuration parameters must contain the same key words as the attributes. Of course you can customize this by tweaking the decorator.
Update
As @rebus correctly pointed out you also need to investigate Django's generic views.
毕竟 Django 是 python,所以你可以轻松地做到这一点:
Django is python after all, so you can easily do this: