python - 无法解析剩余部分:来自 'url_for('static', filename='main.png')' 的 '('static', filename='main.png')'
问题描述
我是django
初学者,我正在尝试渲染html
页面。我已经有一个测试 html 页面和 URL 来测试它。
在点击 URL 时,遇到以下错误:
Could not parse the remainder: '('static', filename='main.png')' from 'url_for('static', filename='main.png')'
完整的跟踪:
Request Method:
GET
Request URL:
http://127.0.0.1:8000/xxxxx
Django Version: 3.0.2
Exception Type: TemplateSyntaxError
Exception Value: Could not parse the remainder: '('static', filename='main.png')' from 'url_for('static', filename='main.png')'
Exception Location: F:\Anaconda\lib\site-packages\django\template\base.py in __init__, line 662
Python Executable: F:\Anaconda\python.exe
Python Version: 3.7.0
我尝试过使用单引号和双引号,但它仍然不起作用。有点卡在这一点上。
任何评论/建议/提示/链接到指南表示赞赏。
解决方案
看看这个。您必须确保在模板中加载静态,{% load static %}
然后您可以让 django 通过使用生成模板中文件的链接{% static "static/path.ext" %}
因此,您将文件更改为:
.bg-image {
background-image: url("{% static "main.png" %}");
filter: blur(8px);
-webkit-filter: blur(8px);
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
只要记住放在{% load static %}
文件的顶部。
推荐阅读
- html - 移动设备的媒体查询适用于模拟的移动设备,但不适用于我的手机
- javascript - 在 React 中模拟 DOM onchange 行为
- git - 是否有 TFVC 签入政策使 TFVC 要求用户在签入前执行获取最新信息?
- pre-commit.com - 了解如何使用 repo 配置预提交:本地
- python - 从计数表计算相对频率
- spring - 找不到能够从 AbstractJpaQueryTupleConverterTupleBackedMap 类型转换为自定义 Java POJO 类的转换器
- c - 当前推荐在 ESP-IDF 项目中包含 AWS Embedded C SDK 的方法?
- android - Android MediaSessionCompat onMediaButtonEvent 不起作用
- raspberry-pi - 通过 USB 将文件复制到 Ender 3 SD 卡,无需 Octipi/Octoprint
- node.js - 使用 Express (nodejs) 在运行时声明路由