gpt4 book ai didi

python - 基本 Django 模板中未定义 STATIC_URL

转载 作者:太空狗 更新时间:2023-10-29 18:13:58 28 4
gpt4 key购买 nike

我有一个模板,base.html,它在其他几个模板中用于各种 View 。这些模板中的每一个都以适当的 {% extends "base.html"%} 开头。在基本模板中,我想这样指定一个静态样式表:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/base.css"/>

然而,当它呈现我的大部分模板时,STATIC_URL 的值是空的,所以该属性只是 href="/base.css",这并不'加载。该变量是为我绑定(bind)到默认登录 View 的模板正确定义的,django.contrib.auth.views.login,但对于我自己的自定义 View ,它是未定义的。

我只是想通过 runserver 让它在开发环境中工作,所以 CSS 文件位于应用程序的静态子目录中。

这是我的 settings.py 中的相关位,它们都是默认值:

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)

在我的 urls.py 中我还添加了:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

#...

urlpatterns += staticfiles_urlpatterns()

知道我做错了什么吗?据我所知,这是基于 the 1.3 documentation 在开发中为特定于应用程序的静态文件提供服务应该做的事情。 .

最佳答案

也许这可以帮助:

If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template.As a brief refresher, context processors add variables into the contexts of every template. However, context processors require that you use RequestContext when rendering templates. This happens automatically if you're using a generic view, but in views written by hand you'll need to explicitly use RequestContext To see how that works, and to read more details, check out Subclassing Context: RequestContext.

关于python - 基本 Django 模板中未定义 STATIC_URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8361538/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com