Webapp2 - TypeError:get()只取1个参数(给定2个)

时间:2017-02-28 11:00:43

标签: python-2.7 google-app-engine jinja2 webapp2

我有一个显示咨询清单的/咨询页面。我的列表循环如下所示:

{% for consult in consults %}
 <tr>
  <td><a href="/consults/view-consult?key={{consult.key.urlsafe()}}">{{ consult.consult_date }}</a></td>
  <td>{{ consult.consult_time }}</td>
  <td>{{ consult.patient_first }}</td>
  <td>{{ consult.patient_last }}</td>
  <td><span class="badge badge-warning">{{ consult.consult_status }}</span></td>
 </tr>
{%endfor%}

所以我使用url将Consult键发送到单个页面以显示有关该咨询的信息。这形成了一个这样的网址:

http://localhost:8080/consults/view-consult?key=aghkZXZ-Tm9uZXIVCxIIQ29uc3VsdHMYgICAgIDIkwoM

当我点击链接时出现错误:

TypeError: get() takes exactly 1 argument (2 given)

应用信息

我的webapp2对象有路径:

('/consults/view-consult(.*)', ViewConsultPage)

此路线的我的RequestHandler:

class ViewConsultPage(webapp2.RequestHandler):
    def get(self):
    template = JINJA_ENVIRONMENT.get_template('/templates/view-consult.html')  
    self.response.out.write(template.render())

app.yaml处理程序:

- url: /consults/view-consult(.*)
  script: main.app

修改

Consults对象模型定义如下:

class Consults(ndb.Model):

# Basic Consult Info (To get started storing a consult in the datastore)

    # Timestamp consult submitted to datastore
    consult_created = ndb.DateTimeProperty(auto_now_add=True)
    # Consult booking date
    consult_date = ndb.StringProperty()
    # Consult booking time
    consult_time = ndb.StringProperty()
    # Provider booking the consult
    consult_user = ndb.StringProperty()
    # Consult status: (Pending, Completed, Cancelled)
    consult_status = ndb.StringProperty(choices=('Pending','Completed','Cancelled'),default='Pending')

# Patient Info

    # The patient's first name
    patient_first = ndb.StringProperty()
    # The patient's last name
    patient_last = ndb.StringProperty()
    # The patient's email address
    patient_phone = ndb.StringProperty()
    # The patient's phone number
    patient_email = ndb.StringProperty()
    # The patient's age in years
    patient_age = ndb.IntegerProperty()
    # Does the patient agree to emails from JW?
    patient_optin = ndb.BooleanProperty()

# Clinical Info

    # Does the patient use an orthodic?
    clin_ortho = ndb.BooleanProperty()
    # Foot type:(Over Pronated, Moderatly Pronated, Neturtal, Supinated, Orthosis)
    clin_type = ndb.StringProperty(choices=('Over Pronated','Moderately Pronated','Neutral','Supinated','Orthosis'))

/ consults页面的RequestHandler:

class ConsultsPage(webapp2.RequestHandler):
    def get(self):
        consults = Consults.query().fetch(5)
        consults_dic = {"consults" : consults}
        template = JINJA_ENVIRONMENT.get_template('/templates/consults.html')
        self.response.out.write(template.render(**consults_dic))
    def post(self):
        booking_date = self.request.get("booking_date")
        booking_time = self.request.get("booking_time")
        first_name = self.request.get("first_name")
        last_name = self.request.get("last_name")
        phone_number = self.request.get("phone_number")
        email_address = self.request.get("email_address")
        age = int(self.request.get("age"))
        opt_in = self.request.get("opt_in") == 'on'
        has_ortho = self.request.get("has_ortho") == 'on'
        foot_type = self.request.get("foot_type")
        consult = Consults( consult_date=booking_date,
                            consult_time=booking_time,
                            patient_first=first_name,
                            patient_last=last_name,
                            patient_phone=phone_number,
                            patient_email=email_address,
                            patient_age=age,
                            patient_optin=opt_in,
                            clin_ortho=has_ortho,
                            clin_type=foot_type)
        consult.put()

回溯

Traceback (most recent call last):
  File "C:\dev\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\dev\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\dev\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\dev\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\dev\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\dev\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\dev\projects\jw-connect\main.py", line 89, in get
    self.response.out.write(template.render())
  File "C:\dev\google-cloud-sdk\platform\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "C:\dev\projects\jw-connect\templates\view-consult.html", line 1, in top-level template code
    {% extends "/templates/base.html" %}
UndefinedError: 'consult' is undefined

1 个答案:

答案 0 :(得分:1)

只需从正则表达式模式中删除捕获组(以及该组中存在的.* )。

('/consults/view-consult', ViewConsultPage)

仅当您想要将网址的某些部分传递给处理程序时才使用捕获组。

例如,

('/consults/([^/]*)', ViewConsultPage)

如果您对此/consults/foo网址发出GET请求,则应调用ViewConsultPage处理程序,并将捕获的字符串foo传递给处理程序的get函数。

def get(self, part):
    print part # foo

对于这种情况,您可以通过self.request.get func轻松获取传递给该网址的参数和值,其中self.request包含所有输入参数及其值。

class ViewConsultPage(webapp2.RequestHandler):
    def get(self):
        key = self.request.get('key', None)
        print key