7

So,

can I somehow stop django rendering specific template variables?

Background is, that I wanted to try vuejs in a django app, which kind of worked.

Problem is, both share the same syntax for variables.

So in vuejs you declare them like

{{ message }}

And djangos template engine interprete it as a django template variable and try to render it. Because it doenst exist, it disappear and vuejs isnt working anymore.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Jakub Juszczak
  • 7,126
  • 3
  • 21
  • 38

1 Answers1

15

According to Vue's docs, its template tag delimiters can be changed to something else, like {!! !!}, if you want.

If that is not an option, Django has a {% verbatim %} tag that can escape parts of your template containing Vuejs tags.

Community
  • 1
  • 1
Brian
  • 7,394
  • 3
  • 25
  • 46
  • I swear I thought there's a way to change Django's tags as well, but I cannot find it. Maybe I am thinking of Jinja2? (Which also works with Django) – Two-Bit Alchemist Nov 23 '15 at 19:18
  • OK, it appears that's Jinja2. Django has [this to say](https://docs.djangoproject.com/en/1.8/ref/templates/api/#limitations-with-string-literals): "Django’s template language has no way to escape the characters used for its own syntax." There are a couple of inconvenient workarounds listed at that link. – Two-Bit Alchemist Nov 23 '15 at 19:21
  • 1
    @Two-BitAlchemist [it's hard coded](https://github.com/django/django/blob/master/django/template/base.py#L99) – Brian Nov 23 '15 at 19:23
  • thanks thats it! I guess changing vues tags is the best solution. – Jakub Juszczak Nov 23 '15 at 19:45
  • It seems like `{% verbatim %}` is the most elegant way to use Django with Vue. – Qback Mar 16 '18 at 11:31