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 许可协议。转载请注明出处!
相关文章
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 分钟
解决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 分钟
八位堂猎户座X-Pro手柄开箱
这个手柄是在之前介绍奥丁2时背景里出现的,也是6月份和飞智那款差不多时间入的,事实证明这玩意就是一分钱一分货,这款的扳机震动和充电底座的体验还是不错的。
2025-10-28 · 手柄 · 约 1 分钟
新盟M75-Pro键盘开箱
某人误选静音轴,手感如按棉花,反馈全无,真是失策。新盟M75-Pro屏幕精致,做工尚可,却因音量旋钮和键帽割手而减分,价格虽香,但轴体选择失误。
2025-10-28 · 键盘 · 约 1 分钟
讨论
共 0 条加载评论中…