第一个web.py表单不起作用

时间:2011-07-23 10:36:43

标签: python web.py

每次尝试此操作时,浏览器都会出现错误

app.py

import web
urls = (
'/hello','/file_upload_form','Index'
)

app = web.application(urls, globals())

render = web.template.render('templates/', base = "layout")

class Index(object):
    def POST(self):
        uploaded_file = web.input(uploaded_file_loc = 'file')
        form = web.input(name = "Nobody", greet = None)
        greeting = "%s, %s" % (form.greet, form.name)
        return render.index(greeting = greeting)
    def GET(self):
        return render.hello_form()
        return render.file_upload_form()


if __name__ == "__main__":
    app.run()

file_upload_form.html

<div id='upload_form'>
<center>
<form action="/file_upload" method="POST">
File to upload:<input type="text" name="uploaded_file_loc">
<br />
<input type="submit">
</form>
</center>
</div>

hello_form.html

<div id="header">
<h1>Company</h1>
</div>

<div id="form">
<form action="/hello" method ="POST">
greeting: <input type="text" name="greet">
<br />
name: <input type="text"name="name">
<br />
<input type="submit">
</form>
<a href="/file_upload">Upload a file</a>
</div>
<div id="home">
</div>

的layout.html

$def with (content)

<html>
<head>
    <title>Gothons from Planet Percal #25</title>
<style type = "text/css">
body {
        background-color:#bbbbbb;
    }

#form{
        background-color:#008899;
        color: #ffffff;
        width: 200px;
        margin-left:80%;
        border-width: thin;
        border-style: solid;
        border-color: #aaaaaa;
        padding: 10px 10px 450px 40px;
        font-size: 20;
        font-family: "verdana";
    }
#header{
        background-color:#008899;
        color:#ffffff;
        height: 80px;
        margin-left: 0%;
        margin-right: 0%;
        font-size:30;
    }
#home{
        background-color:#008899
        }
#talk{
        background-color:#008899;
        margin-left:20%;
        margin-right:20%;
        border-width:1px;
        border-style: solid;
        border-color:#ffffff;
        color:#ffffff;
        font-family: "verdana";
        font-size:20;
        padding 30px 30px 30px 30px;
        height: 100px;

    }
#upload_form{
        background-color:#008899;
        border-width: 1px;
        border-color:#ffffff;
        padding 20px 20px 20px 20px;
        margin-right: 20%;
        margin-left: 20%;
   }
</style>
</head>
<body>
$:content
</body>
</html>

的index.html

$def with (greeting)
<div id="talk">
<center>
$if greeting:
    I just wanted to say $greeting
$else:
    <em>hello</em>
<br />
</center>
</div>
<div id="link">
<a href = "/hello"> The Form! </a>
</div>

这有什么问题?

回溯:

    Traceback (innermost first)
/usr/local/lib/python2.7/dist-packages/web/application.py in _delegate
            cls = fvars[f] ...
    ▼ Local vars
    Variable    Value
  args  
    []
     f  
     u'/file_upload_form'
     fvars  
     {'Index': <class 'app_1.Index'>, '__builtins__': {'ArithmeticError': <type             'exceptions.ArithmeticError'>, 'AssertionError': <type 'exceptions.AssertionError'>,     'AttributeError': <type 'exceptions.AttributeError'>, 'BaseException': <type 'exceptions.BaseException'>, 'BufferError': <type 'exceptions.BufferError'>, 'BytesWarning': <type 'exceptions.BytesWarning'>, 'DeprecationWarning': <type 'exceptions.DeprecationWarning'>, 'EOFError': <type 'exceptions.EOFError'>, 'Ellipsis': Ellipsis, 'EnvironmentError': <type 'exceptions.EnvironmentError'>, 'Exception': <type 'exceptions.Exception'>, 'False': False, 'FloatingPointError': <type 'exceptions.FloatingPointError'>, 'FutureWarning': <type 'exceptions.FutureWarning'>, 'GeneratorExit': <type 'exceptions.GeneratorExit'>, 'IOError': <type 'exceptions.IOError'>, 'ImportError': <type 'exceptions.ImportError'>, 'ImportWarning': <type 'exceptions.ImportWarning'>, 'IndentationError': <type 'exceptions.IndentationError'>, 'IndexError': <type 'exceptions.IndexError'>, 'KeyError': <type 'exceptions.KeyError'>, 'KeyboardInterrupt': <type 'exceptions.KeyboardInterrupt'>, 'LookupError': <type 'exceptions.LookupError'>, 'MemoryError': <type 'exceptions.MemoryError'>, 'NameError': <type 'exceptions.NameError'>, 'None': None, 'NotImplemented': NotImplemented, 'NotImplementedError': <type 'exceptions.NotImplementedError'>, 'OSError': <type 'exceptions.OSError'>, 'OverflowError': <type 'exceptions.OverflowError'>, 'PendingDeprecationWarning': <type 'exceptions.PendingDeprecationWarning'>, 'ReferenceError': <type 'exceptions.ReferenceError'>, 'RuntimeError': <type 'exceptions.RuntimeError'>, 'RuntimeWarning': <type 'exceptions.RuntimeWarning'>, 'StandardError': <type 'exceptions.StandardError'>, 'StopIteration': <type 'exceptions.StopIteration'>, 'SyntaxError': <type 'exceptions.SyntaxError'>, 'SyntaxWarning': <type 'exceptions.SyntaxWarning'>, 'SystemError': <type 'exceptions.SystemError'>, 'SystemExit': <type 'exceptions.SystemExit'>, 'TabError': <type 'exceptions.TabError'>, 'True': True, 'TypeError': <type 'exceptions.TypeError'>, 'UnboundLocalError': <type 'exceptions.UnboundLocalError'>, 'UnicodeDecodeError': <type 'exceptions.UnicodeDecodeError'>, 'UnicodeEncodeError': <type 'exceptions.UnicodeEncodeError'>, 'UnicodeError': <type 'exceptions.UnicodeError'>, 'UnicodeTranslateError': <type 'exceptions.UnicodeTranslateError'>, 'UnicodeWarning': <type 'exceptions.UnicodeWarning'>, 'UserWarning': <type 'exceptions.UserWarning'>, 'ValueError': <type 'exceptions.ValueError'>, 'Warning': <type 'exceptions.Warning'>, 'ZeroDivisionError': <type 'exceptions.ZeroDivisionError'>, '__debug__': True, '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", '__import__': <built-in function __import__>, '__name__': '__builtin__', '__package__': None, 'abs': <built-in function abs>, 'all': <built-in function all>, 'any': <built-in function any>, 'apply': <built-in function apply>, 'basestring': <type 'basestring'>, 'bin': <built-in function bin>, 'bool': <type 'bool'>, 'buffer': <type 'buffer'>, 'bytearray': <type 'bytearray'>, 'bytes': <type 'str'>, 'callable': <built-in function callable>, 'chr': <built-in function chr>, 'classmethod': <type 'classmethod'>, 'cmp': <built-in function cmp>, 'coerce': <built-in function coerce>, 'compile': <built-in function compile>, 'complex': <type 'complex'>, 'copyright': Copyright (c) 2001-2011 Python Software Foundation. All Rights Reserved. Copyright (c) 2000 BeOpen.com. All Rights Reserved. Copyright (c) 1995-2001 Corporation for National Research Initiatives. All Rights Reserved. Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam. All Rights Reserved., 'credits': Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands for supporting Python development. See www.python.org for more information., 'delattr': <built-in function delattr>, 'dict': <type 'dict'>, 'dir': <built-in function dir>, 'divmod': <built-in function divmod>, 'enumerate': <type 'enumerate'>, 'eval': <built-in function eval>, 'execfile': <built-in function execfile>, 'exit': Use exit() or Ctrl-D (i.e. EOF) to exit, 'file': <type 'file'>, 'filter': <built-in function filter>, 'float': <type 'float'>, 'format': <built-in function format>, 'frozenset': <type 'frozenset'>, 'getattr': <built-in function getattr>, 'globals': <built-in function globals>, 'hasattr': <built-in function hasattr>, 'hash': <built-in function hash>, 'help': Type help() for interactive help, or help(object) for help about object., 'hex': <built-in function hex>, 'id': <built-in function id>, 'input': <built-in function input>, 'int': <type 'int'>, 'intern': <built-in function intern>, 'isinstance': <built-in function isinstance>, 'issubclass': <built-in function issubclass>, 'iter': <built-in function iter>, 'len': <built-in function len>, 'license': Type license() to see the full license text, 'list': <type 'list'>, 'locals': <built-in function locals>, 'long': <type 'long'>, 'map': <built-in function map>, 'max': <built-in function max>, 'memoryview': <type 'memoryview'>, 'min': <built-in function min>, 'next': <built-in function next>, 'object': <type 'object'>, 'oct': <built-in function oct>, 'open': <built-in function open>, 'ord': <built-in function ord>, 'pow': <built-in function pow>, 'print': <built-in function print>, 'property': <type 'property'>, 'quit': Use quit() or Ctrl-D (i.e. EOF) to exit, 'range': <built-in function range>, 'raw_input': <built-in function raw_input>, 'reduce': <built-in function reduce>, 'reload': <built-in function reload>, 'repr': <built-in function repr>, 'reversed': <type 'reversed'>, 'round': <built-in function round>, 'set': <type 'set'>, 'setattr': <built-in function setattr>, 'slice': <type 'slice'>, 'sorted': <built-in function sorted>, 'staticmethod': <type 'staticmethod'>, 'str': <type 'str'>, 'sum': <built-in function sum>, 'super': <type 'super'>, 'tuple': <type 'tuple'>, 'type': <type 'type'>, 'unichr': <built-in function unichr>, 'unicode': <type 'unicode'>, 'vars': <built-in function vars>, 'xrange': <type 'xrange'>, 'zip': <built-in function zip>}, '__doc__': None, '__file__': '/home/colin/projects/gothonweb/bin/app_1.pyc', '__name__': 'app_1', '__package__': None, 'app': <web.application.application instance at 0xa60488c>, 'render': <web.template.Render instance at 0xa60498c>, 'urls': ('/hello', '/file_upload_form', 'Index'), 'web': <module 'web' from '/usr/local/lib/python2.7/dist-packages/web/__init__.pyc'>}
    handle_class    

            is_class
                 自
         句柄中的/usr/local/lib/python2.7/dist-packages/web/application.py         return self._delegate(fn,self.fvars,args)...         ▼本地变种     变量值     ARGS
    []     FN
    U '/ file_upload_form'     自
         /usr/local/lib/python2.7/dist-packages/web/application.py正在处理中                     return self.handle()...     ▼本地变种     变量值     处理          处理器
    []     自
    

1 个答案:

答案 0 :(得分:4)

错误是由错误的网址参数引起的。我认为应该是:

urls = (
    '/hello', 'Index',
    '/file_upload_form','Index'
)

请参阅http://webpy.org/cookbook/url_handling

  

这个元组的格式是:url-path-pattern,handler-class这个模式将重复,因为定义了更多的url模式。

顺便说一下:

def GET(self):
    return render.hello_form()
    return render.file_upload_form()

你明白第二次回归从未被召唤过吗?