简体   繁体   English

瓶子Python错误404:找不到:'/'

[英]Bottle Python Error 404: Not found: '/'

I am very new to using bottle but whenever I try to run my programs I always get the error Error 404: Not Found '/'. 我是新手使用瓶子,但每当我尝试运行我的程序时,我总是得到错误错误404:未找到'/'。 The app in my example is not fully functional yet but it should at least display something on the screen. 我的示例中的应用程序尚未完全正常运行,但它至少应该在屏幕上显示一些内容。 Even with fully functional programs this happens. 即使有功能齐全的程序,也会发生 There are similar problems asked but none of the solutions in those have worked. 有类似的问题,但没有一个解决方案有效。

import bottle

from cork import Cork
from cork.backends import SQLiteBackend
sb = SQLiteBackend('sasdasd.db', initialize=True)

aaa = Cork(backend=sb)
app = bottle.Bottle()
def post_get(name, default=''):
    return bottle.request.POST.get(name, default).strip()
@bottle.route('/login')
def login():
    return '''
         <form action="/login" method="post">
         Username: <input name="username" type="text" />
         Password: <input name="password" type="password" />
        <input value="Login" type="submit" />
    </form>
'''
@bottle.post('/login')
def login():
    """Authenticate users"""
    username = post_get('username')
    password = post_get('password')
    aaa.login(username, password, success_redirect='/', fail_redirect='/login')

bottle.run()

As @Wooble points out in his comment, you need to register the route "/" if you expect your webapp to respond with anything other than a 404 for that path. 正如@Wooble在他的评论中指出的那样,如果您希望您的webapp响应该路径的404以外的任何内容,则需要注册路由“/”。 Here's some code to illustrate: 这里有一些代码来说明:

@bottle.get('/')
def home():
   return 'Hello!'

Now your webserver will respond with an HTTP 200 and a body of "Hello!" 现在,您的网络服务器将以HTTP 200和“Hello!”的主体进行响应。 when you request /. 当你要求/。

I realize this question is already answered for the OP, but I was having the same problem with a different cause. 我意识到OP已经回答了这个问题,但是我遇到了同样的问题。 Make sure your run command is after your route statements, not before, or you will get 404 errors for everything. 确保您的run命令在路由语句之后,而不是之前,或者您将获得404错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM