Home  >  Q&A  >  body text

python flask 静态资源404问题

flask 起的本地服务,static 文件夹里面的css 一直是404

template 里面引用的代码如下:

{% block head %}
    {{ super() }}
    <link rel="stylesheet" href="{{ url_for('static', filename='css/base.css') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/cms.css') }}">
{% endblock %}

目录如下:

报错如下:

so上找了很久,都没有结果,求大神救救俺

ringa_leeringa_lee2742 days ago949

reply all(4)I'll reply

  • 高洛峰

    高洛峰2017-04-18 09:45:20

    The reference in the template is a relative reference, and I didn’t specify itstatic的路径, 当然还是404, 所以用之前把static目录位置source

    Just hit static路径写到PYTHONPATH

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:45:20

    Your root directory should not be the app, it should be the level above the app. url_forIt is static in the project root directory

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:45:20

    url_for added _external=True is an absolute reference.

    reply
    0
  • 阿神

    阿神2017-04-18 09:45:20

    I programmed a routing test myself and found no such problem...
    Have you posted the other codes in the HTML to take a look

    Also I would like to ask, if the poster puts the css file in the static folder, will it run successfully?

    @main.route('/test')
    def test():
        return render_template('test.html')
        

    test.css is placed in the static/test folder

    * {
        color: red;
    }
    

    The html file is written here

    <!DOCTYPE html>
    <head>
    <link rel="stylesheet" type="text/css" href="{{url_for('static',filename='test/test.css')}}" >
    </head>
    
    <p>Test it</p>
    
    

    reply
    0
  • Cancelreply