为什么我在Flask中得到404错误? [英] Why am I getting 404 error in Flask?

查看:768
本文介绍了为什么我在Flask中得到404错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Django开发人员学习Flask。对于Flask中的工作,我很新。当我访问 localhost:5000 时,出现以下代码。有人可以解释为什么我得到这个:

在服务器上找不到请求的URL。
如果您手动输入网址,请检查您的拼写,然后重试。


  from flask import Flask 
from flask.ext.sqlalchemy import SQLAlchemy
$ b $ from datetime import datetime
$ b app = Flask(__ name__)
app.config ['SQLALCHEMY_DATABASE_URI'] ='sqlite:////media/workdrive/workspace/purkinje/temp.db'
app.debug = True
db = SQLAlchemy(app)

if __name__ = ='__main__':
app.run()
$ b $ class Post(db.Model):
id = db.Column(db.Integer,primary_key = True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
pub_date = db.Column(db.DateTime)

category_id = db.Column(db.Integer,db.ForeignKey('category.id'))
category = db.relationship('Category',backref = db.backref('posts',lazy ='dynamic '))

def __init __(self,title,body,category,pub_date = None):
self.title = title
self.body = body
如果pub_date是None:
pub_date = datetime.utcnow()
self.pub_date = pub_date
self.category =类别

def __repr__ (self):
return'< Post%r>'%self.title

class类别(db.Model):
id = db.Column(db.Integer ,primary_key = True)
name = db.Column(db.String(50))
$ b $ def __init __(self,name):
self.name = name

def __repr __(self):
return'< Category%r> %self.name'

@ app.route('/')
def index():
posts = Post()。query.all()
return render_template('templates / index.html',posts = posts)


解决方案 app.run()。如果将__name__ =='__main __'块全部移动到脚本的底部,则应将整个移动到脚本的底部。


I'm a Django developer learning Flask. I'm pretty new to how things work in Flask. I'm getting a 404 with the following code when I visit localhost:5000. Could someone explain why I'm getting this:

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

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////media/workdrive/workspace/purkinje/temp.db'
app.debug = True
db = SQLAlchemy(app)

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

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80))
    body = db.Column(db.Text)
    pub_date = db.Column(db.DateTime)

    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    category = db.relationship('Category', backref=db.backref('posts', lazy='dynamic'))

    def __init__(self, title, body, category, pub_date=None):
        self.title = title
        self.body = body
        if pub_date is None:
            pub_date = datetime.utcnow()
        self.pub_date = pub_date
        self.category = category

    def __repr__(self):
        return '<Post %r>' % self.title

class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return '<Category %r> % self.name'

@app.route('/')
def index():
    posts = Post().query.all()
    return render_template('templates/index.html', posts=posts)

解决方案

You're calling app.run() before registering your handler. You should move the whole if __name__ == '__main__' block to the bottom of the script.

这篇关于为什么我在Flask中得到404错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆