【问题标题】:static Invalid block tag inside template file模板文件中的静态块标记无效
【发布时间】:2019-12-06 03:40:26
【问题描述】:

我在尝试从我的应用程序文件结构中引用 styles.css 文件时遇到了无效的块标记问题,即使已将所有内容配置为 django 文档(以及堆栈溢出中的其他几个答案)。

我在 python3.6 venv 中使用 django 2.2.3 以及 pureCSS lib。

以下是关于模板和静态目录/文件的项目文件和配置的概述:

1- settings.py INSTALLED_APPSSTATIC_URL 定义:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pyonlinecourses.core',
]
...
STATIC_URL = '/static/'

2- settings.py 模板定义:

TEMPLATES_DIR = os.path.join(BASE_DIR, "pyonlinecourses/core/templates/")
TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATES_DIR],
    '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',
            'django.core.context_processors.static',
            ],
        },
    },
]

3- Django项目目录结构:

- pyonlinecourses
  - core
    - migrations
    - static
      - css
    - templates
      - home **(issue located in this file)**

5-static标签定义和html文件中的引用

<!doctype html>
{& load static &}
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="PyOnline Courses - Simple distance education virtual learning environment" />
  <title>PyOnline MOOC</title>
  <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css">
  <link rel="stylesheet" href="{% static 'css/styles.css' %}" />
</head>

我在试图找出问题的根源时不知所措。

【问题讨论】:

  • 您是否将DEBUG 设置为False
  • 不,它设置为True,因为我还在开发它
  • 虽然不是针对这个问题,但请注意,在生产环境中,静态文件由 Django 提供,您需要配置 nginx/apache 来执行此操作。
  • 我知道这一点,我什至在发布问题之前尝试更改 DEBUG 选项,并了解如何在生产中完成它。但谢谢你注意到它。

标签: python django django-templates python-3.6


【解决方案1】:

你有{&amp; load static &amp;}而不是{% load static %}

【讨论】:

    猜你喜欢
    • 2011-10-22
    • 2015-01-28
    • 2016-04-12
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2014-01-09
    • 2011-12-18
    • 2018-10-22
    相关资源
    最近更新 更多