javascript - 将 chessbord.js 与 Flask 集成

标签 javascript python flask chessboard.js

我正在使用 Flask,但在使用 chessboard.js 时遇到问题有了它。

我的静态文件夹是这样的:

/static/
    /css/
    /img/
    /js

我放置 chessboard.js 文件的位置。

我的模板:

{% extends "base_templates/page_base.html" %}  {# base_templates/page_base.html extends base_templates/base.html #}

{% block main %}
<h1>Home page</h1>

<p>This page is accessible to any user.</p>

{% if not current_user.is_authenticated() %}
<p>
To view the Member page, you must
<a href="{{ url_for('user.login') }}">Sign in</a> or
<a href="{{ url_for('user.register') }}">Register</a>.
</p>
{% endif %}

<p>The code:</p>
<p>ChessBoard initializes to an empty board with no second argument.
</p>

<!-- Here the chessboard code: start example HTML --->
<div id="board" style="width: 400px"></div>
<!-- end example HTML --->

<script type="text/javascript" src='/static/js/json3.min.js'></script>
<script type="text/javascript" src='/static/js/jquery.min.js'></script>
<script type="text/javascript" src='/static/js/chessboard.js'></script>
<script>

var init = function() {

//--- start example JS ---
var board = new ChessBoard('board');
//--- end example JS ---

}; // end init()
$(document).ready(init);
</script>
{% endblock %}

JavaScript 找不到用于显示面板的图像。它尝试在/img/而不是/static/img/查找图像。我该如何解决这个问题?

最佳答案

chessboard-0.3.0.js 的第 445 行,它说

cfg.pieceTheme = 'img/chesspieces/wikipedia/{piece}.png';

如果将其更改为:

cfg.pieceTheme = '/static/img/chesspieces/wikipedia/{piece}.png';

这能解决你的问题吗?您可能需要使用确切的路径来反射(reflect)完整的目录结构(或使用相对路径),但这是需要更改的地方。

关于javascript - 将 chessbord.js 与 Flask 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29136916/

相关文章:

python - 找不到 apache2 flask 应用程序 css 文件?

python - Docker容器运行失败,错误: python3: can't open file 'flask run --host=0.0.0.0' : [Errno 2] No such file or directory

javascript - 展开 JS 对象并转换数组

javascript - 无法重定向到jade页面 Node js

python - 子类化时如何不必在 init 中键入 self

Python pandas将列表插入单元格

javascript - 使用 AngularUI 作为热键

javascript - 找到一组随机元素并 appendChild

python - 如何在 Visual Studio Code 中将我的 python 文件导入到另一个文件中

python - 将Docker运行到Kubernetes的问题