Django模板语言无法识别

iszxjhcz  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(90)

我正在开发Django项目。昨天当我离开项目时,我没有遇到任何问题。今天当我打开这个项目时,我的{% load static %}是灰色的,下一行是“<!doctype html>”给了我“意外令牌”。我的项目除了模板外工作得很好。我在虚拟环境中工作,在github上有我的项目:https://github.com/FREDYKRUGE/Mist2.git. P.S我与PyCharm专业合作
我试着再次安装所有的依赖,他们都很满意,试着激活venv,没关系。我的码头已经启动了。
我正在谈论的模板的照片https://imgur.com/a/T0WWpEI

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-composite.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"]


更新:我重新扫描indexe,没有工作,重新打开我的项目,没有工作,丢弃共享索引(不管这意味着什么),它工作了。我通过“主菜单->文件->修复IDE”到达了这一点

oaxa6hgo

oaxa6hgo1#

静态文件的URL是“mysite/static/”
当你想访问你的“style.css”例如你应该使用“/static/style.css”而不是“/mysite/style.css”

编辑:

更改您的settings.py的此部分
绝对路径:

STATICFILES_DIRS = ('C:/django-project/mysite/static')

字符串
或相对:

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


不要忘记设置STATIC_URL!

相关问题