简单的烧瓶应用程序,从.html文件中读取其内容。外部样式表被阻止? [英] Simple flask application that reads its content from a .html file. External style sheet being blocked?

查看:148
本文介绍了简单的烧瓶应用程序,从.html文件中读取其内容。外部样式表被阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个非常简单的烧瓶应用程序,从.html文件读取其内容。该应用程序除了风格的作品。奇怪我的内联CSS代码工作,但不是外部样式表。我检查过的语法,它应该工作。烧瓶是否会阻止读取.css文件?



文件夹中的文件可以在这里。这3个文件都在同一个文件夹中。

解决方案

您的代码不是使用Flask提供的文件,它只是读取文件并将其发送到浏览器 - 这就是为什么URL不起作用。

您需要从方法中呈现文件。



首先在 .py 文件的同一目录中创建一个模板文件夹并移动你的html文件到这个文件夹中。创建另一个名为 static 的文件夹,并将样式表放在该文件夹中。

您应该有

  /flaskwp1.py 
/模板
webcode.html
/静态
webcodestyle.css
$ / code>

然后,调整您的代码:

<$ p $ () - > from flask import Flask,render_template
$ b app = Flask('flaskwp1')
#webcode = open('webcode.html')。不需要

@ app.route('/')
def webprint():
return render_template('webcode.html')

if __name__ =='__main__':
app.run(host ='0.0.0.0',port = 3000)

编辑 webcode.html

 < link rel =stylesheet
type =text / css
href =/ static / webcodestyle.css/>


I made a very simple flask application that reads its content from a .html file. The application works except for the style. Strangely my inline css code works but not the external style sheet. I've checked the syntax, it should work. Does flask somehow prevent the .css file from being read?

The files in the folder can be viewed here. Those 3 files are all in the same folder.

解决方案

Your code is not serving files using Flask, it is simply reading a file and sending it to the browser - which is why URLs are not working.

You need to render the file from within the method.

First make a templates folder in the same directory as your .py file and move your html file into this folder. Create another folder called static and put your stylesheet in that folder.

You should have

/flaskwp1.py
/templates
  webcode.html
/static
  webcodestyle.css

Then, adjust your code thusly:

from flask import Flask, render_template

app = Flask('flaskwp1')
# webcode = open('webcode.html').read() - not needed

@app.route('/')
def webprint():
    return render_template('webcode.html') 

if __name__ == '__main__':
    app.run(host = '0.0.0.0', port = 3000)

Edit webcode.html:

<link rel="stylesheet"
      type="text/css"
      href="/static/webcodestyle.css"/>

这篇关于简单的烧瓶应用程序,从.html文件中读取其内容。外部样式表被阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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