简体   繁体   English

django 如何在模板中包含 javascript

[英]django how to include javascript in template

I'm starting a project and following the documentation I didn't succeed to include javascript.我正在开始一个项目并按照文档我没有成功地包含 javascript。

Here is my settings:这是我的设置:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

STATIC_ROOT = '/static/'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

So I have a static folder create in my project with a javascript file.所以我在我的项目中创建了一个带有 javascript 文件的静态文件夹。

myproject/static/app.js我的项目/静态/ app.js

my urls.py:我的 urls.py:

urlpatterns = [
    url(r'^$', 'app.views.home'),
    url(r'^admin/', include(admin.site.urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

and in my template: this is myproject/templates/base.html:在我的模板中:这是 myproject/templates/base.html:

<!DOCTYPE html>
<head>
  {% load static %}
  <script src="{% static 'app.js' %}"></script>   
  <title>Site</title>
</head>
<body>
<img src="{% static 'img.png' %}" alt="Mon image" />
  {% block content %}{% endblock %}
</body>
</html>

My other template:我的另一个模板:

{% block content %}
    hello world
{% endblock %}

I have the "hello world" on我打开了“你好世界”

http://127.0.0.1:8000/ http://127.0.0.1:8000/

but i do not have my image or script.但我没有我的图像或脚本。

I tried so many different things but I never succeed我尝试了很多不同的事情,但我从未成功

urls.py网址.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
]

settings.py设置.py

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

STATIC_URL = '/static/'

# remove STATIC_ROOT

base.html基本文件

Your title tag was not closed.您的标题标签未关闭。

<!DOCTYPE html>
<head>
  {% load static %}
  <script src="{% static 'app.js' %}"></script>   
  <title>Site</title>
</head>
<body>
<img src="{% static 'img.png' %}" alt="Mon image" />
  {% block content %}{% endblock %}
</body>
</html>

Your template should say {% load staticfiles %} instead of {% load static %}你的模板应该说{% load staticfiles %}而不是{% load static %}

Source: https://docs.djangoproject.com/en/1.8/howto/static-files/来源: https : //docs.djangoproject.com/en/1.8/howto/static-files/

Also, os.path.join(BASE_DIR, "static"), only looks for static files in your apps, as in app/static/app/static.js .此外, os.path.join(BASE_DIR, "static"),仅在您的应用程序中查找静态文件,如app/static/app/static.js If you have static files that do not belong to any specific app, but rather to the project, you need to add the folder explicitly.如果您的静态文件不属于任何特定应用程序,而是属于项目,则需要明确添加文件夹。 See point 4 of 'Configuring static files' on the docs page I mentioned.请参阅我提到的文档页面上“配置静态文件”的第 4 点。

From my understanding, the STATICFILES_DIRS should be in the settings.py and defined like this:根据我的理解, STATICFILES_DIRS应该在settings.py并定义如下:

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

Please check the documentation .请检查文档

I am new at programing thus take my points with a pinch of salt.我是编程新手,因此请稍加保留我的观点。 There i go.我去。 maybe you have more apps meaning you gotta check your tree as it should go as the django documentations points.也许你有更多的应用程序意味着你必须检查你的树,因为它应该按照 django 文档的要点进行。 meaning that you should have your_app/static/your_app/in here you must put three more folders corresponding to css, images and js all with their respective files.这意味着你应该有 your_app/static/your_app/ 在这里你必须再放三个对应于 css、images 和 js 的文件夹以及它们各自的文件。

Also notice that your static url should be named diferent than your static root.另请注意,您的静态 url 应命名为与静态根不同的名称。 Once you have done this you must tell django where to find the statics for each app thus you gotta state it within the STATICFILES_DIRS.完成此操作后,您必须告诉 django 在哪里可以找到每个应用程序的静态数据,因此您必须在 STATICFILES_DIRS 中声明它。

After all this then you gotta run the collectstatic command.完成所有这些之后,您必须运行 collectstatic 命令。

I´m still missing the part in which you connect the js files of each app to the template.. so I cannot further help you dude.... hope it helps a little bit我仍然缺少将每个应用程序的 js 文件连接到模板的部分。

Create directory named 'static' at base directory. i.e. at similar level to project directory.
Then add following in settings.py

STATIC_URL = '/static/'
#setting for static files
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM