Django:是否不可能将静态标记转换为块标记?
下面的代码出错。。我如何解决这个问题Django:是否不可能将静态标记转换为块标记?,django,static,block,Django,Static,Block,下面的代码出错。。我如何解决这个问题 {% block header %} <link rel="stylesheet" href="{% static 'shop/style.css' %}" /> {% endblock %} {%block header%} {%endblock%} 错误输出: TemplateSyntaxError:无效的块标记:“static”,应为“endblock” 不,这不是不可能的。尝试将{%load staticfiles%}包含在同
{% block header %}
<link rel="stylesheet" href="{% static 'shop/style.css' %}" />
{% endblock %}
{%block header%}
{%endblock%}
错误输出:
TemplateSyntaxError:无效的块标记:“static”,应为“endblock”
不,这不是不可能的。尝试将
{%load staticfiles%}
包含在同一html文件中,而不是尝试从某些base.html
1.)中继承它。在settings.py中添加一个元组:
from django.contrib.staticfiles.urls import staticfiles.urlpatterns
urlpatterns += staticfile_urlpatterns()
{% load static from staticfiles %}
and then use :
<link rel="stylesheet" href="{% static 'assets/css' %}"
统计文件\u目录=(
join(BASE_DIR,'assets'),
)
2.)在url.py中添加:
from django.contrib.staticfiles.urls import staticfiles.urlpatterns
urlpatterns += staticfile_urlpatterns()
{% load static from staticfiles %}
and then use :
<link rel="stylesheet" href="{% static 'assets/css' %}"
3.)在放置“link rel='stylesheet'…”的html文件中,只需在顶部添加:
from django.contrib.staticfiles.urls import staticfiles.urlpatterns
urlpatterns += staticfile_urlpatterns()
{% load static from staticfiles %}
and then use :
<link rel="stylesheet" href="{% static 'assets/css' %}"
{%loadstaticfromstaticfiles%}
然后使用:
对。Django不会允许的
您可以使用适当的路径,如:
<link rel="stylesheet" href="/static/shop/style.css" />
但请注意:如果更改应用程序的静态URL
,则上面的href
也必须相应更新
从:
在模板中,要么硬编码url,如/static/my_app/example.jpg,要么最好使用静态模板标记
我的解决方案是包含另一个带有{%load static%}
的页面和带有静态引用的脚本{%block xxx%}
期望第一个{%yyy%}
不是{%include%}
和{%endblock%}
(我观察到的唯一情况);因此,当我们使用“{%static'xxx.js%}”
时,它会中断并抱怨。但加上另一页会让Django平静下来
例如,我有一个扩展了base.html
的页面homepage
,并且有一些不包含在base.html
中的静态js文件
base.html
{% block page %}
{% endblock %}
{% block script %}
{% endblock %}
homepage.html
:
{% extends 'base.html' %}
{% block page %}
...
{% endblock %}
{% block script %}
{% include 'home_js.html'%} <!-- don't use static links here because Django does not like it. -->
{% endblock %}
{% load static %}
<script src="{% static 'scripts/jquery.js' %}" ></script>
<script>
function ...
</script>
现在脚本加载
因此,在块中,除了{%block xxx%}
和{%endblock%}
之外,我们不能使用{%endblock%}
标记
我正在使用Django5.1
编辑:
在这种情况下,我发现{%verbatim%}
标记是我们的救星。只要在{%extends'app/base.html%}
之后将{%load static%}
添加到模板顶部,如果您使用的是Apache,请确保您已将虚拟主机配置为静态文件服务,例如在000 default.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin webmaster@localhost
Alias /static /home/Dev/cfehome/src/static
<Directory /home/Dev/cfehome/src/static>
Require all granted
</Directory>
<Directory /home/Dev/cfehome/src/cfehome>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess cfehome python-path=/home/Dev/cfehome/src:/home/Dev/cfehome/lib/python3.7/site-packages
WSGIProcessGroup cfehome
WSGIScriptAlias / /home/Dev/cfehome/src/cfehome/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
服务器名www.example.com
服务器管理员webmaster@localhost
别名/static/home/Dev/cfehome/src/static
要求所有授权
要求所有授权
WSGIDaemonProcess cfehome python path=/home/Dev/cfehome/src:/home/Dev/cfehome/lib/python3.7/site-packages
WSGIProcessGroup cfehome
WSGIScriptAlias//home/Dev/cfehome/src/cfehome/wsgi.py
ErrorLog${APACHE_LOG_DIR}/error.LOG
CustomLog${APACHE\u LOG\u DIR}/access.LOG组合
加载了{%load staticfiles%}
吗?好的,@nextdoordoc。您可以发布您当前的模板结构吗。@karthikr是正确的,这是必要的,即使您的模板继承自base.html文件,该文件中已经有{%load static%}标记,这是否意味着对于每个模板,我都必须添加此标记?此属性不会在子模板中继承?如果从某些base.html
扩展,则这不是一个好选项,因为这些base.html希望在关闭
之前输出所有静态文件,并且这些静态文件可能因页面而异。谢谢!我刚刚通过你的回答解决了我的问题+1为解决方案。但它应该包括我们是否在base中加载静态文件。没有完全理解逻辑,或者您可以使用{%load static%}
,也可以。