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

狂风中的少年 提交于 2019-12-25 09:06:05

问题


I have a /consults page displaying a list of consults. My list loop looks like this:

{% 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%}

So I'm using the url to send a Consult key to the individual page to display info about that consult. This forms a url like this:

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

When I click the link I get an error:

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

App Info

My webapp2 object has the route:

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

My RequestHandler for this route:

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

app.yaml handler:

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

Edit:

The Consults object model is defined as follows:

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'))

And the RequestHandler for the /consults page:

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

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:


Just remove the capturing group (and also the .* present inside that group) from your regex pattern.

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

Use capturing group only if you want to pass some part of your url to the handler.

For example,

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

If you make a GET request to this /consults/foo url, it should invoke the ViewConsultPage handler and the captured string foo should be passed to the handler's get function.

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

For this case, you can easily get the parameters and values passed to that url by self.request.get func where self.request holds all the input params along with their values.

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


来源:https://stackoverflow.com/questions/42506944/webapp2-typeerror-get-takes-exactly-1-argument-2-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!