2017-06-03 68 views
0

我经历了许多教程和一切工作。但是我开始了自己的网站,只找到404错误。错误404与静态页面(烧瓶框架)

我下载了一个HTML模板,并设法将文件从Github克隆到PythonAnywhere。现在我的文件结构是: Screenshot

Flask_app.py代码:

from flask import Flask 

# set the project root directory as the static folder, you can set others. 
app = Flask(flask_app, static_url_path='/home/dubspher/mysite') 

app = Flask(__name__) 
@app.route('/home/dubspher/mysite/') 
def static_file(path): 
    return app.send_static_file(index.html) 

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

我应该创建模板,另一个网站,并使用render_template或有一个办法可以要求一个静态页面烧瓶?

到目前为止,我尝试了许多代码,但我不知道需要什么来与static_url_path做

HTML:https://www.pythonanywhere.com/user/dubspher/shares/db715c9b9d7c43b7beb4245af23fb56c/

让我知道,如果我需要增加更多的信息。

+0

你看看https://stackoverflow.com/questions/20646822/how-to-serve-static-files-in-flask – clockwatcher

+0

是的这段代码直接来自这个页面。 –

+0

@ app.route定义了请求中瓶子将要查找的url路径。那么你要求什么网址?您必须要求http://yoursite.com/home/dubspher/mysite/才能打到您的索引页面。这是你要求的还是你要求http://yoursite.com?如果是后者,则需要将路由定义为@ app.root('/')。此外,index.html需要在引号中。 – clockwatcher

回答

0

这里是你的代码搞掂你的网站(如http://example.com/)的根就可以提供的index.html:

from flask import Flask 

# set the project root directory as the static folder, you can set others. 
app = Flask(__name__, static_folder='/home/dubspher/mysite/') 

@app.route('/') 
def static_file(): 
    return app.send_static_file('index.html') 

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

那假设你在你的/家庭/ dubspher有一个index.html文件/ mysite目录。

为了跟进你的css问题,你可以通过将一个static_url_path传递给Flask构造函数来让瓶子提供静态文件。当你这样做的时候,匹配static_url_path的任何请求都会被视为一个静态文件,并根据static_folder路径进行提交。在下面的示例中,我将static_url_path设置为static,将static_folder设置为/ home/dubspher/mysite /。当请求http://example.com/static/css/site.css进入烧瓶时,将提供文件/home/dubspher/mysite/css/site.css。

from flask import Flask 

app = Flask(__name__, static_url_path="/static", static_folder='/home/dubspher/mysite/') 

@app.route('/') 
def static_file(): 
    return app.send_static_file('index.html') 

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

下面是引用的index.html在/home/dubspher/mysite/css/site.css样式表的一个例子。

<html> 
<head> 
<link rel="stylesheet" type="text/css" href="/static/css/site.css"> 
</head> 
<body> 
Hello There 
</body> 
</html> 

如果你使用的static_url_path,你必须非常小心,你的static_folder路径下的一切是你想要的东西访问。 Flask将按原样提供服务。

+0

太棒了!非常感谢! –