【问题标题】:Cherrypy 404 errorCherrypy 404 错误
【发布时间】:2014-06-03 19:46:34
【问题描述】:

404 未找到

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>

如何让路径可以正确访问?

【问题讨论】:

  • 所以当您选择下拉菜单时出现错误?该错误看起来像您的请求 /getCustomer 会返回一个错误,因为它没有公开。
  • 我没解决,还是谢谢你

标签: python cherrypy


【解决方案1】:

唯一让我印象深刻的是在您的模板中您引用了 customerTuple,但在您的渲染命令中您使用的是 customerList。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2010-10-20
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多