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 分钟
StarBlog博客开发笔记(10):图片展示-瀑布流-首页卡片
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetCore开发博客项目 StarBlog - (3) 模型设计 基于.NetCore开发博客项目 StarBlog - (4) markdown博客批量导入 基于.NetCore开发博客项目 StarBl
2022-11-22 · StarBlog博客开发笔记 · 约 1 分钟
给Django-Admin添加验证码和多次登录尝试限制
Django自带的Admin很好用,但是放到生产环境总还差了点什么= = 看看admin的介绍: Django奉行Python的内置电池哲学。它自带了一系列在Web开发中用于解决常见问题或需求的额外的、可选工具。这些工具和插件,例如django.contrib.redirects都必须在settings中的INSTALLED_APPS处进行注册,有的还需要执行manage.py migrate命令
2021-12-18 · Django · 约 1 分钟
讨论
共 0 条加载评论中…