Python django 如何在模板中包含 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30313314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django how to include javascript in template
提问by BoumTAC
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
我打开了“你好世界”
but i do not have my image or script.
但我没有我的图像或脚本。
I tried so many different things but I never succeed
我尝试了很多不同的事情,但我从未成功
采纳答案by Charlesthk
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>
回答by Jorick Spitzen
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
. 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.
此外,os.path.join(BASE_DIR, "static"),
只在您的应用程序中查找静态文件,如app/static/app/static.js
. 如果您的静态文件不属于任何特定应用程序,而是属于项目,则需要明确添加文件夹。请参阅我提到的文档页面上“配置静态文件”的第 4 点。
回答by Victor Orgaz
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. 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.
我是编程新手,因此请稍加保留。我去。也许你有更多的应用程序意味着你必须检查你的树,因为它应该按照 django 文档的要点进行。这意味着你应该有 your_app/static/your_app/ 在这里你必须再放三个对应于 css、images 和 js 的文件夹以及它们各自的文件。
Also notice that your static url should be named diferent than your static root. 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.
另请注意,您的静态 url 应命名为与静态根不同的名称。完成此操作后,您必须告诉 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 文件连接到模板的部分.. 所以我不能进一步帮助你老兄.. 希望它有点帮助
回答by canishk
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.
请检查文档。