You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django模板语言未被PyCharm识别的问题及解决方法

问题:PyCharm中Django模板识别异常,项目运行正常

我使用PyCharm专业版开发Django项目,昨日离开时项目一切正常,今日打开后发现模板中的{% load static %}显示为灰色,下一行的<!doctype html>提示“Unexpected tokens”。项目本身运行无异常,仅模板出现识别问题。我采用虚拟环境开发,项目托管在GitHub,相关模板有对应截图。

已尝试以下操作但未解决问题:

  • 重新安装所有依赖(依赖均已满足)
  • 激活虚拟环境(状态正常)
  • 确保Docker处于运行状态
  • 重新扫描索引、重启项目

settings.py 内容

import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

DEBUG = True

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Mist2.common',
    'Mist2.games',
    'Mist2.accounts',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'Mist2.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates']
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'Mist2.wsgi.application'

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "MistDB",
        "USER": "postgres-user",
        "PASSWORD": "password",
        "HOST": "127.0.0.1",
        "PORT": "5432",
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '/static/'),
)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

AUTH_USER_MODEL = 'accounts.MistUser'

docker-compose.yml 内容

version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_DB: MistDB  # Replace 'MistDB' with the same value you used in settings.py
      POSTGRES_USER: postgres-user  # Replace 'postgres-user' with the same value you used in settings.py
      POSTGRES_PASSWORD: password  # Replace 'password' with the same value you used in settings.py
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    ports:
      - "5432:5432"

  web:
    build:
      context: D:\SoftuniProjects\Mist2  # The path to your Django project directory
      dockerfile: Dockerfile  # Use the newly created Dockerfile for building the Django web application image
    command: python manage.py runserver 0.0.0.0:8000  # Adjust this command based on your Django setup
    volumes:
      - .:/app  # Mount the current directory (Django project) to the '/app' directory in the container
    ports:
      - "8000:8000"  # Map host port 8000 to container port 8000

volumes:
  postgres_data:

Dockerfile 内容

FROM python:3.11.4

# Install PostgreSQL development packages
RUN apt-get update && apt-get install -y libpq-dev gcc

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install the required dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Start the Django development server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

解决方法

更新:尝试重新扫描索引、重启项目均无效,最终通过「主菜单→文件→修复IDE」清除共享索引后,问题解决。


内容的提问来源于stack exchange,提问作者Momchil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 03:38:13