Quote Confusion

This is line 3 of base.html in the current Django repo on GitHub,

<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" dir="{{ LANGUAGE_BIDI|yesno:'rtl,ltr,auto' }}">

Naturally, PyCharm flags this, and it is easy to see why. Any beginner Python text on strings will tell you that you can use either single or double quotes, but if they overlap, you should wrap double inside single, or vice versa. The same rule applies to html. Here the double quote after html lang is closed on the other side of the }}, making the use of double quotes inside to wrap en-us trigger the PyCharm warning.

However, the error message itself says nothing about quotes, single or double. Clicking on the first one moves the cursor to the colon between default and the next double quote in the string, saying it expected }} - which is correct, since the {{ came right after the first double quote.

I thought it was odd to see such errors in source code, but went ahead with what both PyCharm and I knew was right. But despite all that, none of this is really correct in this instance. In fact, it will throw a big fat 500 error until you put it all back the way it came. And all the traceback will tell you is that there is a TemplateSyntaxError.

Apparently this has something to do with the template language, perhaps overriding all good sense, I don’t know, but that is why I am posting. Could someone here please explain this error and syntax to me?

Thank you.

I’m not sure what error you’re referring to here.

Keep in mind that this template is data being read from a file, it is not a string literal being assigned to a Django variable. It’s an arbitrary stream of characters.

To understand what’s happening, you need to look at how the Django template rendering engine is going to look at this.

This line will be processed as a sequence of characters:
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" dir="{{ LANGUAGE_BIDI|yesno:'rtl,ltr,auto' }}">

What’s going to happen is that the rendering engine is going to find the variable identifiers {{ ... }} and process what’s inside it as a reference to the context. In this case, the first string is: {{ LANGUAGE_CODE|default:"en-us" }}, which is going to be rendered (perhaps) as en-us, which then in the larger context will end up creating:
<html lang="en-us" ...

I can’t address what PyCharm might be telling you. There’s nothing wrong with this template, so my suggestion would be to ignore it.