Django:将 url 映射到类,就像 web.py 中一样

发布于 2024-12-04 09:55:29 字数 352 浏览 2 评论 0原文

我正在学习 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

i am learning django but gave web.py a try first.
while reading django's documentation i found that in i need to check for the request type in each method.. like:

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

can the web.py type classes be implemented in django like

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

it would be super cool

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

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

发布评论

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

评论(1

九厘米的零° 2024-12-11 09:55:30

是的,这可以通过新的(如 Django 1.3)基于类视图

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基类,有许多视图适合各种情况,例如TemplateView或表单视图。 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

Yes, that's possible with the new (as in 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

Usually, you don't have to use the View base class, there are many views that are geared towards all kinds of cases, e.g. TemplateView or FormView. Reinout van Rees has two excellent blog posts that go into the details:

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

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