2017-02-28 81 views
0

我有一個/諮詢頁面顯示諮詢列表。我的列表循環是這樣的:Webapp2 - TypeError:get()只需要1個參數(2給出)

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

編輯:

的諮詢誰對象模型被定義如下:

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

而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

只需卸下捕獲組從正則表達式模式(,也是本.*該組內側)。

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

僅當您想要將網址的一部分傳遞給處理程序時才使用捕獲組。

例如,

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

如果你犯了一個GET請求,此/consults/foo URL,它應該調用ViewConsultPage處理程序和捕捉串foo應該傳遞給處理程序的get函數。

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

對於這種情況,你可以輕鬆地獲得通過self.request.get FUNC其中self.request持有所有輸入它們的值PARAMS傳遞到URL中的參數和值。

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

工作。偉大的信息Avinash。那麼這個鍵現在可以用於View Consult頁面嗎? – TimothyAURA

+0

我試圖通過向RequestHandler添加一行來定義諮詢方法 class ViewConsultPage(webapp2。RequestHandler): def get(self): \t self.request.get('key',None) \t ** consult = ndb.Key(urlsafe = request.get('key')).get()* * 但出現錯誤:NameError:全局名稱'請求'未定義 – TimothyAURA

+0

它是'self.request'而不是'request'。是的。看到我的更新.. –

相關問題