Django - javascript不会在html页面上加载

问题描述 投票:0回答:2

我的Django网站不会加载javascript文件,但它对css部分做得很好,即使它们都在同一个Static文件夹中。

base.html文件:

<head>
    {% load static %}
    <meta charset="UTF-8">
    <link href="{% static 'login/css/bootstrap.css' %}" rel="stylesheet" type="text/css">
    <link href="{% static 'login/css/bootstrap.min.css' %}" rel="stylesheet" type="text/css">
    <link href="{% static 'login/css/bootstrap-theme.css' %}" rel="stylesheet" type="text/css">
    <link href="{% static 'login/css/bootstrap-theme.min.css' %}" rel="stylesheet" type="text/css">
    <script src="{% static 'login/js/bootstrap.js' %}"></script>
    <script src="{% static 'login/js/bootstrap.min.js' %}"></script>

    <title>LOGIN</title>
</head>

settings.朋友:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    ]

这是静态文件夹的路径:

/home/nikola/Desktop/GIA/static

我搜索过谷歌,解决方案非常通用,比如urls.py或python manage.py collectstatic(仅用于部署),没有任何帮助。我一无所知。

什么似乎是问题?

javascript python css django static
2个回答
0
投票

正在加载js文件。我认为你没有看到js插件正常工作,因为你没有在head块中加载jquery文件。毕竟,bootstrap js插件依赖于jquery来工作。

此外,在大多数实践中,应该在结束</body>标记之前在HTML文件的末尾加载js文件,因为它们往往很大,并且您希望浏览器在加载js文件之前首先加载较小的资源。因此,您的结构应该类似于:

<html>
    <head>
        <!-- meta stuff here -->
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script> <!-- jquery should always load before any other scripts -->
        <link href="css1.css" rel="stylesheet">
        <link href="css2.css" rel="stylesheet">
        ...
        ...
    </head>
    <body>
        ...
        ...
        <script type="text/javascript" src="{% static 'login/js/bootstrap.min.js' %}"></script>
    </body>
</html>

0
投票
  1. 确保settings.py中的DEBUG = True
  2. 保持每个应用程序中的静态文件夹,当它开发时,Django将搜索每个应用程序下的静态文件。只发布到生产ENV,使用python manage.py collectstatic的静态文件夹将在web服务器配置中进行配置。如果您使用的是nginx,etc / nginx / sites-enabled / config_file: root /.project_home/django_root; location /static/ { alias /.project_home/django_root/static/; }
© www.soinside.com 2019 - 2024. All rights reserved.