【问题标题】:Django:Map urls to class like in web.pyDjango:将 url 映射到 web.py 中的类
【发布时间】:2011-09-10 09:51:57
【问题描述】:

我正在学习 django,但首先尝试了 web.py。 在阅读 django 的文档时,我发现我需要检查每个方法中的请求类型.. 比如:

def myview():
  if request.method == "POST":
    #blah balh 
    #ke$ha (jst kiddn)
  else:
    #(balh)x2

web.py 类型的类可以像 django 一样实现吗

class myView():
 def GET(self):
   #cool
 def POST(self):
   #double cool

一定很酷

【问题讨论】:

    标签: python django web web.py


    【解决方案1】:

    是的,新的(如在 Django 1.3 中)class-based views 可以做到这一点:

    from django.views.generic.base import View
    
    class MyView(View):
    
        def get(self, request, *args, **kwargs):
            # return a response here
    
        def post(self, request, *args, **kwargs):
            # return a response here
    

    通常,您不必使用View 基类,有许多视图适用于各种情况,例如TemplateViewFormView。 Reinout van Rees 有两篇优秀的博文详细介绍:

    http://reinout.vanrees.org/weblog/2011/08/24/class-based-views-walkthrough.html

    http://reinout.vanrees.org/weblog/2011/08/24/class-based-views-usage.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 2016-09-21
      相关资源
      最近更新 更多