当前位置: 首页 > 面试题库 >

Flask-找不到404

葛永丰
2023-03-14
问题内容

python flaskr.py

我得到的是一个404 Not Found错误的说法

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

这是文件内的代码

import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash

#create app
app = Flask(__name__)
app.config.from_object(__name__)

#load default conf and override config from an env var
app.config.update(dict(
    DATABASE=os.path.join(app.root_path, 'flaskr.db'),
    DEBUG=True,
    SECRET_KEY = 'dev key',
    USERNAME = 'admin',
    PASSWORD = 'admin'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)

def connect_db():
    """connect to the specific db"""
    rv = sqlite3.connect(app.config['DATABASE'])
    rv.row_factory = sqlite3.Row
    return rv

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


def get_db():
    """opens a new db connection if there is none yet for the cyrrent app"""
    if not hasattr(g, 'sqlite_db'):
        g.sqlite_db = connect_db()
        return g.sqlite_db

@app.teardown_appcontext
def close_db(error):
    """closes the db again at the end of the request."""
if hasattr(g, 'sqlite_db'):
    g.sqlite_db.close()

def init_db():
    with app.app_context():
        db = get_db()
        with app.open_resource('schema.sql', mode='r') as f:
            db.cursor().executescript(f.read())
        db.commit()


@app.route('/')
def show_entries():
    db_get_db()
    cur = db.execute('select title, text from entries order by id desc')
    entries = cur.fetchall()
    return render_template('show_entries.html', entries=entries)

@app.route('/add', methods=['POST'])
def add_entry():
    if not session.get('logged_in'):
        abort(401)
    db = get_db()
    db.execute('insert into entries (title, text) values (?,?)',
        [request.form['title'], request.form['text']])
    db.commit()
    flash('New entry was successfully posted')
    return redirect(url_for('show_entries'))

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != app.config['USERNAME']:
            error = 'Invalid username'
        elif request.form['password'] != app.config['PASSWORD']:
            error = 'Invalid password'
        else:
            session['logged_in'] = True
            flash('You were logged in')
            return redirect(url_for('show_entries'))
    return render_template('login.html', error=error)

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out')
    return redirect(url_for('show_entries'))

这是我收到的控制台消息(加上3次刷新页面的尝试):

user@user:~/Flask/flaskr$ python flaskr.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [19/Aug/2014 15:23:40] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Aug/2014 15:23:41] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Aug/2014 15:23:42] "GET / HTTP/1.1" 404 -

关于可能出什么问题的任何建议?


问题答案:

app.run()太早了:

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

这是在你的任何路由被注册之前执行的。将这两行到结束你的文件。

接下来,你输入的第一行show_entries()不正确:

def show_entries():
    db_get_db()

没有db_get_db()功能;应该这样db = get_db()



 类似资料:
  • 问题内容: 我正在使用Flask,一个适用于Python的网络框架。Flask使用Jinja渲染模板。我不知道Jinja Flask使用哪个版本,也不知道如何检索Jinja版本或Flask版本。我使用python版本2.7。 该模板在css / image目录中包含一个图像。直接在Firefox浏览器中以文件形式查看模板时,此图像可见。 但是在执行Flask时不是: HTML文件的内容: 模板运行

  • 问题内容: 大家好,我只是在学习烧瓶。我用pip来安装它。然后,当我运行此基本代码时,我得到一个错误。基本上,我看到它的工作,然后突然退出,并出现以下错误。这可能看起来是一些环境问题,但我不确定。前几天这很奇怪,现在却行不通。 问题答案: 您说过,仅当您从交互式shell运行代码时,才会出现此问题。它是由(wsgi服务器所基于的)功能引起的。 如果更改了项目文件,则在调试模式下将自动重新启动服务器

  • 我是一名学习使用jsp和Servlet构建Web应用程序的学生。一个月以来,我的Web应用程序项目一直运行良好,但今天它的行为突然变得奇怪了。当我提交jsp页面时,它无法找到我的servlet。我已经使用servlet注释来映射请求。 以下是我的JSP:- 以下是我的servlet:- 以下是我的控制台日志:-

  • 我指的是一个问题,已经被问了很多次,但其他地方发布的解决方案没有解决我的问题ie Socket.io.js找不到。 错误消息为 获取http://127.0.0.1:3000/socket.io/socket.io.js 404 一些附加信息:在我的app.js中:我引用了服务器套接字 '/javascripts/sockets/client.js'是我的客户端套接字:

  • 我对Tomcat这玩意儿真的很陌生。我下载了Tomcat7.0 windows installer并使用默认配置安装了它。安装后,我在浏览器中键入localhost:8080,查看Tomcat是否工作。但是,它显示了这样的错误消息:Access error:404--Not Found不能定位文档:/并且页面中没有Tomcat或Apache单词显示任何其他内容。看来Tomcat没有反应。 我谷歌搜

  • 我正在尝试使用pyzbar 0.1。4在Docker中的Flask服务器上 该图像由我们创建,基于取自alpine的python 2.7。 通过以下方式安装ZBar 我在运行dockerfile