【问题标题】:Flask can't load css fileFlask 无法加载 css 文件
【发布时间】:2015-06-15 19:58:20
【问题描述】:

下面的模板有问题:没有加载 CSS 文件。

我将“style.css”正确放置在“my_project/static/”中。

我只是尝试将HTML文件源代码中的<link rel=stylesheet ...>替换为“style.css”的代码,它可以工作。

有什么问题?

<!doctype html>
<title>{% block title %}Welcome{% endblock %} | MiniTwit</title>
<link rel=stylesheet type=text/css href="{{  url_for('static', filename='style.css') }}">

<div class=page>
  <h1>MiniTwit</h1>
  <div class=navigation>
  {% if g.user %}
    <a href="{{ url_for('timeline') }}">my timeline</a> |
    <a href="{{ url_for('public_timeline') }}">public timeline</a> |
    <a href="{{ url_for('logout') }}">sign out [{{ g.user.username }}]</a>
  {% else %}
<a href="{{ url_for('public_timeline') }}">public timeline</a> |
<a href="{{ url_for('register') }}">sign up</a> |
<a href="{{ url_for('login') }}">sign in</a>
  {% endif %}
  </div>
  {% with flashes = get_flashed_messages() %}
    {% if flashes %}
      <ul class=flashes>
      {% for message in flashes %}
        <li>{{ message }}
      {% endfor %}
      </ul>
    {% endif %}
  {% endwith %}
  <div class=body>
  {% block body %}{% endblock %}
  </div>
  <div class=footer>
    MiniTwit &mdash; A Flask Application
  </div>
</div>

【问题讨论】:

  • 为什么体外有一个div? - 那是蟒蛇吗?
  • 请发布您的应用程序文件夹结构和应用程序配置的最小示例。这闻起来像是 Flask 没有正确解析静态文件的路径。

标签: jquery python html css flask


【解决方案1】:

您应该在link 标签中的属性值两边加上双引号,并将此标签放在head 标签内:

<head>
<title>{% block title %}Welcome{% endblock %} | MiniTwit</title>
<link rel="stylesheet" type="text/css" href="{{  url_for('static', filename='style.css') }}">
</head>

同时关闭你的 html 文件末尾的 html 标签。

【讨论】:

  • 我已经这样做了,但是没有用。我上面写的代码是一些关于使用 Flask 进行 Web 编程的书中的示例代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 2016-01-21
  • 1970-01-01
相关资源
最近更新 更多