Solving Django and Vue.js Template Syntax Conflicts
Problem Django templates and Vue.js both wrap variables with {{ and }}. When rendering a Django template, all {{ and }} and the content wrapped in them are replaced first, so Vue.js cannot use {{ and }} to bind variables. Solutions Method 1: Change Vue.js's default binding delimiters Configure when creating the Vue App let app = new Vue({ delimiters: [[[, ]]], el: '#vu
目录6 个章节
Problem
Django templates and Vue.js both wrap variables with {{ and }}. When rendering a Django template, all {{ and }} and the content wrapped in them are replaced first, so Vue.js cannot use {{ and }} to bind variables.
Solutions
Method 1: Change Vue.js's default binding delimiters
Configure when creating the Vue App
let app = new Vue({
delimiters: ["[[", "]]"],
el: '#vue-app',
// ...
})
Or change it globally
Vue.config.delimiters = ["[[", "]]"];
After doing this, you can use [[ and ]] to bind variable data
Method 2: Use template tags to output {{ and }}
Django's templatetag can output template tags when rendering the template. The tag arguments and their output are as follows:
| Argument | Output |
|---|---|
openblock | {% |
closeblock | %} |
openvariable | {{ |
closevariable | }} |
openbrace | { |
closebrace | } |
opencomment | {# |
closecomment | #} |
We can use the following in the template
{% templatetag openvariable %}
{% templatetag closevariable %}
to replace {{ and }}
Method 3: Disable Django template rendering
Django's verbatim tag keeps the code wrapped inside it from being rendered, outputting it as-is
{% verbatim %}
{{ vue }}
{% endverbatim %}
We can wrap the parts needed for Vue.js with {% verbatim %}{% endverbatim %}.
finally
This project is a simple online exam system I made in 2020
In the end I used the first method
The code snippet is as follows
<div class="card-header d-flex justify-content-between">
<div>
第<b>[[currentShowIndex]]</b>题
<span class="text-primary font-weight-bold" v-if="current.type==1">单选</span>
<span class="text-primary font-weight-bold" v-if="current.type==2">多选</span>
<span class="text-primary font-weight-bold" v-if="current.type==3">判断</span>
</div>
<div id="timer"></div>
</div>
_EOF_
Solving Django and Vue.js Template Syntax Conflicts
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
解决Django使用Jinja2模版引擎之后admin无法访问的问题
需要两个文件 说明:这两个文件放在哪里都是ok,但是要记住路径,待会配置的时候需要输入。 建议放在和settings.py同个目录下面。 创建backends.py文件: import sys from django.template.backends import jinja2 as jinja2backend from django.template.backends.utils impor
2022-05-27 · 0_Django开发常见问题 · 约 1 分钟
CSRF-verification-failed-Request-aborted
About the CSRF_Token (From Django Official Document) The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries. This type of attack occurs when a malici
2022-04-02 · 0_Django开发常见问题 · 约 1 分钟
洗澡时该不该关电热水器?电小酷16A智能插座开箱
买了16A智能插座,结果发现配不上电热水器,笑死。当初为了安全考虑,现在看来纯属多余。电小酷这产品,够尴尬吧?
2025-10-28 · 其他 · 约 1 分钟
解决 VSCode 的自动换行问题
VSCode 的 Markdown 自动换行看似简单,中文字体一上场就开始装死。问题根源是 `simple` 策略计算失准,改用 `advanced`,再配合 `wordWrap: on`,终于不用拖横向滚动条欣赏长句了。
2026-09-05 · IDE · 约 1 分钟
讨论
共 0 条加载评论中…