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

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

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

{% 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 将咨询键发送到单个页面以显示有关该咨询的信息。这形成了一个像这样的 url:

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

当我点击 link 时出现错误:

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

编辑:

咨询对象模型定义如下:

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

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

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

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

例如,

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

如果您向此 /consults/foo url 发出 GET 请求,它应该调用 ViewConsultPage 处理程序并将捕获的字符串 foo 传递给处理程序的 get功能。

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

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

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