Django:将 url 映射到类,就像 web.py 中一样
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这可以通过新的(如 Django 1.3)基于类视图:
通常,您不必使用
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:
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
orFormView
. 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