1

I have an HTML file which is a Django template. For the most part, it's pretty boring, there's a bit of HTML and some Javascript code inside script tags.

The problem is that I've inserted a single line of Django template language (with the double curly braces), and it cascades into a thousand different errors on every line. How do I ignore this? All I can find is // @ts-ignore on Google which doesn't seem to work with HTML files.

I don't even know where to begin. Is this a linting issue? What linter am I using, which documentation should I look at, etc. I assume I should be using the default tools for javascript. Please help!

The line in question is:

var achievementFlag = {{ achievement_flag|yesno:"true,false" }};

Naturally, the double curly braces is bad, as is the | and the :. And now the javascript just has squiggles all over it.

4
  • 1
    Have you tried setting the language mode for the file in VS Code? Near the bottom right of the screen is the selected language. If you click on that, you can change the language. If Django template is not available, change the language to html (twig) - seems to be pretty close. code.visualstudio.com/docs/languages/… Commented Dec 8, 2019 at 0:51
  • The extensions for Django templates take away more features than they add in, so it hasn't been all that helpful. I was really hoping there'd be a simple way to ignore a line. Currently I'm just commenting it out while I work on the rest of the code, but that's not a great way to tackle it in the long run. Commented Dec 8, 2019 at 7:32
  • 1
    Try the html(twig) extension. It doesn't take away much - it may not match Django 100%, but I believe you will be able to keep the js in the file Commented Dec 9, 2019 at 0:35
  • Sorry for the slow reply, but initially one of the Twig extensions was pretty horrible, but Twig Language 2 by mblode is fantastic and is now working as I'd like. Wasn't the solution I expected, but thanks a ton! Commented Dec 14, 2019 at 23:53

2 Answers 2

7

Add this line to settings.json

"html.validate.scripts": false,

this line will make vscode ignores javascript validation in HTML files

credits: https://github.com/Microsoft/vscode/issues/17433#issuecomment-273870328

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way only work for django template validation not all js code?
ok, I see django template extension solving this issue, thanks
0

Another workaround I usually use is to use JSON.parse:

var achievementFlag = JSON.parse('{{ achievement_flag|yesno:"true,false" }}');

2 Comments

JSON.parse will throw an error with the value that you are passing ({{ achievement_flag|yesno:"true,false" }})
The expression inside double curly braces will be replaced by Django by an actual "true" or "false" value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.