【问题标题】:method in python that does post and getpython中确实发布和获取的方法
【发布时间】:2014-06-04 20:37:17
【问题描述】:

我创建了一个方法,它接收带有参数的请求以及 url 中的 2 个附加参数,url 看起来像这样:

url(r'^courseid=(?P<course_id>\d+)/user_id=(?P<user_id>\d+)/$', views.user_data_on_course, name='userDataOnCourse'),

函数如下所示

def user_data_on_course(request, course_id, user_id):
   if request.method == 'POST':
       <Extract the data and save to DB>
   else if request.method == 'GET':
       <Return the data>

当我添加 else if request.method == 'GET': 时,每个被调用的方法都会出现错误 500,而 DEBUG 信息是 Exception Value: invalid syntax (views.py, line 308),这是 else if 的行

我做错了什么?

【问题讨论】:

    标签: python http-post get-request


    【解决方案1】:

    在 python 中使用elif 而不是else if

    这里是文档:https://docs.python.org/2/tutorial/controlflow.html

    【讨论】:

      【解决方案2】:

      python if 语句结构为:

      if a:
          b
      elif c:
          d
      else:
          e
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-05
        • 2014-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多