Cherrypy 404错误

时间:2014-06-03 09:24:38

标签: python cherrypy

404 Not Found

The path '/getCustomer' was not found.

Traceback (most recent call last):
File "C:\Python33\virtualenviorments\cherrypro\lib\site-packages\cherrypy\_cprequest.py",      line 670, in respond
response.body = self.handler()
File "C:\Python33\virtualenviorments\cherrypro\lib\site-packages\cherrypy\lib\encoding.py", line 212, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "C:\Python33\virtualenviorments\cherrypro\lib\site-packages\cherrypy\_cperror.py", line 411, in __call__
raise self
cherrypy._cperror.NotFound: (404, "The path '/getCustomer' was not found.")

当我尝试显示从下拉列表中选择的用户表时出现此错误....这里是py代码

import cherrypy 
import sqlite3 
from jinja2 import Environment, FileSystemLoader 
fileSystemLoader = FileSystemLoader('templates') 
env = Environment(loader = fileSystemLoader) 

class Root: 

@cherrypy.expose 
def index(self):
    self.getNames() # For the drop down menu 
    tmpl = env.get_template('index6.html') 
    return tmpl.render(salutation ='Welcome to', 
                        target =' Supershop', 
                        myList = self.nameList) 
@cherrypy.expose 
def getOneCustomer(self, customerName = None): 
    if str(customerName) =='Select': # Nobodys name selected 
        custList = ['', 'Select name'] 
    else: 
        custList = self.getCustomer(customerName)[0] 
    self.getNames() # For the drop down menu 
    tmpl = env.get_template('index6.html') 
    return tmpl.render(salutation = 'Welcome to', 
                        target = ' Supershop', 
                        myList = self.nameList, 
                        customerList = custList) 

def connect(self): 
    # Get the connection to the database 
    self.conn=sqlite3.connect('supershop') 
    # Get the cursor object for the database 
    self.cursor=self.conn.cursor() 

def getNames(self): 
    self.nameList = [] # Reset the nameList 
    self.connect() # Make connection to DB and get cursor 
    # Select the name column from the database 
    self.cursor.execute(""" 
        SELECT customerTable.name 
        FROM customerTable ORDER BY customerTable.name """) 
    self.conn.commit() 
    # Fill the names into the nameList 
    for row in self.cursor.fetchall(): # Get tuple of tuples 
        for field in row: # Iterate over "name tuple" 
            self.nameList.append(field) 
    self.cursor.close() 
    self.conn.close() 

def getCustomer(self, customerName): 
    self.connect() # Make connection to DB and get cursor 
    self.cursor.execute(""" 
        SELECT customerTable.idCust, customerTable.name, 
        customerTable.email, customerTable.address, customerTable.city 
        FROM customerTable WHERE customerTable.name = ? """, 
        (customerName,)) # MUST be a tuple. Don’t forget the comma! 
    self.conn.commit() 
    self.DBcustList = list(self.cursor.fetchall()) 
    self.cursor.close() 
    self.conn.close() 
    return self.DBcustList 

cherrypy.config.update({'server.socket_host': '127.0.0.1', 
                    'server.socket_port': 8080, 
                    }) 

cherrypy.quickstart(Root()) 

这里是html代码

<h1>{{salutation}}{{target}}</h1>
<p>Select a name from thee list:</p>

<form action='getOneCustomer' method='post'>
<u1>
<select name = 'customerName'>
        {%for n in myList%}
        <option>{{n}}</option>
        {%endfor%}
</select>
</u1>
        <p><input type='submit' value='Submit'/></p>
</form>
<p>Customer data:</p>
<table style='width:300px'>
<tr>
    <th>Id</th>
    <th>Name</th>
    <th>Email</th>
    <th>Address</th>
    <th>City</th>
</tr>
<tr>
    {%for n in customerTuple%}
    <td>{{n}}</td>
    {%endfor%}
</tr>
</table>

如何正确访问路径?

1 个答案:

答案 0 :(得分:0)

唯一能说明问题的是您在模板中引用customerTuple但是在渲染命令中使用customerList。

希望这有帮助!