基于请求方法的django视图调度工具
django_view_dispatch的Python项目详细描述
用法
fromdjango.conf.urlsimportpatterns,urlfromdjango_view_dispatchimportdispatch,dispatch_strictfrom.importviewsurlpatterns=patterns('',url(r'^some_url/$',dispatch(get=views.my_view,post=views.my_view_for_post),name='events_json'),# you can get any keyword argument you want to specify any http verburl(r'^some_url/$',dispatch(get=views.my_view,put=views.create,foo=views.another_view,bar=views.baz),name='events_json'),# by default, if a request has an HTTP verb that hasn't been specified in# the dispatch() function, it will be redirected to the GET view if presenturl(r'^some_url/$',dispatch(get=views.will_get_everything_thats_not_put,put=views.stuff),name='events_json'),# you can change this behavior this wayurl(r'^some_url/$',dispatch(get=views.stuff,put=views.will_get_everything_thats_not_get,default="put"),name='events_json'),# if "default" is set to None, this behavior is disabled and and# HttpResponseNotAllowed will be returnedurl(r'^input/$',dispatch(post=views.my_view_for_post,default=None),name='events_json'),# behave like @require_POST# a more explicit way to do that is provided with "dispatch_strict" which behave exactly like dispatch with default set to Noneurl(r'^input/$',dispatch_strict(post=views.my_view_for_post),name='events_json'),)
安装
pip install django_view_dispatch
为什么
你在编程过程中学到的东西是 条件是“坏的”(如“应该避免”)因为它们是 潜在的错误来源。
例如,在同一视图中混合POST和GET处理可能是一个不错的选择。 地形的错误和更复杂的代码,而分裂它给出了 KISS情况 你有两种观点,它们只做一件事,而且做得很好。django并没有提供一个简单的或标准的方法来实现这一点 图书馆正试图解决这个问题。
我可以在url函数中使用字符串吗?
不,自从django 1.8以来,在模式is depreciated中使用字符串作为目标视图, 因此,支持这一特性是没有意义的。
而且,老实说,我不知道该怎么做,也不知道 做过任何研究。这也会使代码更加 复杂。
运行测试
pip install pytest py.test test.py
许可证
疯牛病。