【问题标题】:Webapp2 - TypeError: get() takes exactly 1 argument (2 given)Webapp2 - TypeError: get() 只需要 1 个参数(给定 2 个)
【发布时间】:2017-07-19 07:37:02
【问题描述】:

我有一个显示咨询列表的 /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 将咨询密钥发送到单个页面以显示有关该咨询的信息。这形成了一个这样的网址:

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

【问题讨论】:

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


    【解决方案1】:

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

    ('/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
    

    【讨论】:

    • 那行得通。伟大的信息阿维纳什。那么这个密钥现在可以用于查看咨询页面吗?
    • 我试图通过在 RequestHandler 类 ViewConsultPage(webapp2.RequestHandler) 中添加一行来定义咨询对象: def get(self): self.request.get('key', None) consult = ndb.Key(urlsafe=request.get('key')).get() 但出现错误:NameError: global name 'request' is not defined
    • 好的,现在我得到了错误:文件“C:\dev\projects\jw-connect\templates\view-consult.html”,第 1 行,在顶级模板代码 {% extends "/templates/base.html" %} UndefinedError: 'consult' is undefined ...页面看不到在处理程序中创建的咨询对象。
    • 您的咨询对象是什么样的?贴出对应的处理程序代码。
    • 我在原帖的Edit行之后添加了有关咨询的信息。
    猜你喜欢
    • 2017-09-15
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多