Search code examples
django

Invalid Token Error with Static Files in Django Template


I'm encountering an issue with my Django project where I'm trying to include a static CSS file in my HTML template. Despite trying multiple solutions, I keep getting the error:

ERROR: Invalid Token. Expected stringEnd but found tag start "{%" instead.

Here's my HTML template:

{% load static %}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Image</title>
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
  </head>
  <body>
    <h1>This is a pic</h1>
    <img src="{% static 'images/pic.jpg' %}" alt="pic">
  </body>
</html>

I getting this error on this line <link rel="stylesheet" href="{% static 'css/style.css' %}"> by hovering on this line on my editor(vs code).

Solutions I've Tried:

Placed {% load static %} at the top. Verified no syntax errors. Checked static settings in settings.py. Confirmed template backend setup. Ensured correct directory structure. Restarted server and cleared cache.


Solution

  • This might be related to the VS Code extension you're using for Django. Try uninstalling and reinstalling the extension.