我在 Django
和 Angular 4
前端中有一个 Web 服务器,但我的问题是我无法在 Angular 模板中使用 Django 标签。
例如
我有这个index.html,就像/templates(django)中的根目录
{% load static %}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>TrackerFrontend</title>
<base href="{% static %}">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link
href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<!--<link
rel="stylesheet"
type="text/css"
href="http://fonts.googleapis.com/css?family=Fontin">
Not work
-->
</head>
<body>
<app-root>Loading...</app-root>
<script type="text/javascript" src="{% static 'inline.bundle.js' %}"></script>
<script type="text/javascript" src="{% static 'polyfills.bundle.js' %}"></script>
<script type="text/javascript" src="{% static 'styles.bundle.js' %}"></script>
<script type="text/javascript" src="{% static 'vendor.bundle.js' %}"></script>
<script type="text/javascript" src="{% static 'main.bundle.js' %}"></script>
</body>
</html>
如果我尝试从我的服务器调用媒体文件,我可以看到这个
<img src="{% get_media_prefix %}/images/2017-08-12-19-28-20st1folio1.png">
但是当我复制并粘贴相同的 img 标签,在我的项目 Angular 中执行 npm run build
并再次运行我的服务器时,我在终端中收到错误
Template parse errors:
Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)
这个index.html是前端应用程序中真实的副本,但我添加了{% load static %}
和其他标签来调用bundle.js
文件。
可以在每个 Angular 组件内部复制调用 {% load static %}
吗?
无需执行npm run build
也会出现相同的错误
最佳答案
改变
<img src="{% get_media_prefix %}/images/2017-08-12-19-28-20st1folio1.png">
至<img src="{{ get_media_prefix }}/images/2017-08-12-19-28-20st1folio1.png">
关于python - 在 Angular 4 模板中使用 django 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45664172/