PythonFlask不显示Javascrip

2024-04-26 23:54:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python flask项目中显示一个javascript文件。我在调用javascript文件的html文件中使用块。它只是没有显示在网页上

如果我删除了项目的base.html部分,并将所有内容都放到snake.html文件中,它运行良好,这对我来说Javascript很好,我只是误解了Flask部分

如果您能给我任何帮助,我将不胜感激

基本.html

<html>
  <head>
    {% if title %}
    <title>{{ title }}</title>
    {% else %}
    <title>Unseen Arcade</title>
    {% endif %}
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style> body {padding: 10; margin: 10;} </style>
    <script src="p5.min.js"></script>
    <script src="addons/p5.dom.min.js"></script>
    <script src="addons/p5.sound.min.js"></script>
    {% block scripts %}{% endblock scripts %}
  </head>
  <body>
    <div>Unseen Arcade: <a href="/index">Home</a></div>
    <hr>
    {% block content %}{% endblock content %}
  </body>
</html>

蛇.html

{% extends "base.html" %}
{% block scripts %}
  <script src="{{url_for('static', filename='snake/snake.js')}}"></script>
{% endblock %}

{% block content %}
  <body>
    <h2> Snake Game:</h2>
    <p>Use the keys W, A, S, and D to control the direction of the snake, if you get onto the leaderboard with a score above 50 you will receive a hint on the locations of other secret arcades in London.$
  </body>
{% endblock %}

文件夹布局:

FlaskApp/
FlaskApp/
    forms.py
    __init__.py
    routes.py
    static/
      p5.min.js
      addons/
        p5.dom.min.js
        p5.sound.min.js
      snake/
        snake.js
    templates/
        base.html
      snake/
        snake.html
  flaskapp.wsgi

Tags: 文件thesrcbasetitlehtmljsscript