The jinja2 annotation problem of stepping on the pit (in Flask)

error message

 jinja2.exceptions.TemplateSyntaxError 

jinja2.exceptions.TemplateSyntaxError: Expected an expression, got 'end of statement block'

reason

Today, when writing a web WeChat crawler with Flask, when it comes to the  render_template() data of the front-end page, it suddenly starts to report deadly errors, and it is useless to adjust the back-end;

However, the point of the question is not the backend, see below for details:

rear end:

front end:

result:

Troubleshoot

Troubleshoot 1

Since there is data when returning, an error will be reported, try not to transmit data!

rear end:

front end:

 

result:

wtf~~~

Obviously still not...

It stands to reason that there is nothing wrong with directly  rendering_template() a single page...

Continue to investigate...

Troubleshoot 2

 Now return the string directly, not render

rear end:

 front end:

Front end... there is no front end...

Because the direct return is a string, so the front end is not used.

result:

Check 3

In summary, it can be basically concluded that it is the problem of the template file, that is, the problem of the index.html file;

Try to modify the content of index.html:

rear end:

No data is passed, only one page is rendered;

front end:

Clean and tidy, just an h1 tag and text content

result:

 

Summarize

To sum up the comparison, it can be basically concluded that the code that was commented out by me is at fault;

The content of the comment is what I commented directly on the pycharm shortcut key. For html, this comment is fine;

But jinja2 does not recognize the grammar!

The jinja2 grammar has its own annotation method; that is to say, the crux of the problem is the "annotation" method in the jinja2 grammar;

jinja2 annotation method

In jinja2 { #   #} represents a comment, where the content is not escaped by the template engine.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326628254&siteId=291194637