为什么 Django 使用 django.views.static.serve 缓存我的静态数据?

发布于 2024-10-20 13:06:02 字数 5171 浏览 1 评论 0原文

我在本地开发服务器上设置了 static.serve,但它似乎会缓存静态文件(在我的例子中是 css、javascript 和图像),直到我重新启动服务器。我没有使用 apache,并且我将缓存设置为:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}

一起删除缓存声明似乎也没有帮助。

在我从旧的 1.1 版本升级到 1.2.5 之前,这种情况没有发生。

每次进行样式更新时都必须重新启动开发服务器(通过触摸 python 文件或通过命令行),这是一件很痛苦的事情。

编辑 - 按照建议,我添加了 settings.py 和 url.py

Settings.py

# Django settings for zeiss_elearning project.
from django.utils.translation import ugettext_lazy as _
gettext = lambda s: s

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Jason Roy', '###'),
)

#Email settings

EMAIL_HOST = '###'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = '####'
DEFAULT_FROM_EMAIL = '[email protected]'
MANAGERS = ADMINS

DATABASES = {
'default': {
    'ENGINE' : 'django.db.backends.mysql',   
    'NAME' : '###',
    'USER' : '###',
    'PASSWORD' : '###',
    'HOST' : '/Applications/MAMP/tmp/mysql/mysql.sock',
}
}

TIME_ZONE = 'America/Tijuana'


LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = True

USE_L10N = True

MEDIA_DEBUG_DOC_ROOT = '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/media'
MEDIA_ROOT = '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/media'

MEDIA_URL = '/static_files/'

ADMIN_MEDIA_PREFIX = '/media/admin/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '####'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'cms.middleware.page.CurrentPageMiddleware', 
    'cms.middleware.user.CurrentUserMiddleware', 
    'cms.middleware.toolbar.ToolbarMiddleware',
    'cms.middleware.media.PlaceholderMediaMiddleware',
    #'django.middleware.cache.FetchFromCacheMiddleware',
    #'debug_toolbar.middleware.DebugToolbarMiddleware',
)

ROOT_URLCONF = 'zeiss_elearning.urls'

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth', 
    'django.core.context_processors.i18n', 
    'django.core.context_processors.request', 
    'django.core.context_processors.media', 
    'cms.context_processors.media',
)

TEMPLATE_DIRS = (,
    '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/templates',
    '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/cms/templates',
)

SESSION_COOKIE_AGE = 86400

LOGIN_URL = '/membership/login/'

LOGIN_REDIRECT_URL = "/"

AUTHENTICATION_BACKENDS = ( 
    'zeiss_elearning.shared.email_auth.EmailBackend',
    'django.contrib.auth.backends.ModelBackend',
)
AUTH_PROFILE_MODULE = 'membership.UserProfile'
FORCE_SCRIPT_NAME = ''

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'cms',
    'cms.plugins.text',
    'cms.plugins.picture',
    'cms.plugins.link',
    'cms.plugins.file',
    'cms.plugins.snippet',
    'cms.plugins.googlemap',
    'cms.plugins.zeiss_video',
    'cms.plugins.html',
    'cms.plugins.quiz',
    'cms.plugins.popup',
    'mptt',
    'publisher',
    'zeiss_elearning.forms',
    'zeiss_elearning.membership',
    'zeiss_elearning.quiz',
    'menus',
    'south',
)
INTERNAL_IPS = ('127.0.0.1',)

#CMS Settings


CMS_REDIRECTS = True
CMS_MENU_TITLE_OVERWRITE = True
CMS_DBGETTEXT = False

CMS_DEFAULT_TEMPLATE = 'base.html'
CMS_ALLOW_HTML_TITLES = False

CMS_TEMPLATES = (
    ('base.html', _('Default')),
    ('cirrus.html', _('Cirrus')),
    ('atlas.html', _('Atlas')),
)

# Site title for your template
CMS_SITE_TITLE = 'Zeiss Cirrus'

CMS_LANGUAGE_REDIRECT = False

CMS_LANGUAGES = (
    ('en', gettext('English')),
)
LANGUAGES = (
    ('en', gettext('English')),
)
CMS_APPLICATIONS_URLS = (
('zeiss_elearning.quiz.urls', 'Quiz')
)

urls.py

from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings

admin.autodiscover()

urlpatterns = patterns('',
    (r'^membership/', include('zeiss_elearning.membership.urls')),
    (r'^admin/', include(admin.site.urls)),

)
urlpatterns += patterns('',
    url(r'^', include('cms.urls')),
)
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static_files/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_DEBUG_DOC_ROOT}),)

I have static.serve setup on my local development server, but it seems to cache static files (in my case, css, javascript and images) until I restart the the server. I am not using apache, and I have the cache set to:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}

Removing the caches declaration all together doesn't seem to help either.

This didn't happen before I upgraded to 1.2.5 from an older 1.1 version.

It's a pain to have to restart the dev server every time (either by touching a python file or via the command line) every time I make a style update.

Edit - as suggested, I've added settings.py and url.py

Settings.py

# Django settings for zeiss_elearning project.
from django.utils.translation import ugettext_lazy as _
gettext = lambda s: s

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Jason Roy', '###'),
)

#Email settings

EMAIL_HOST = '###'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = '####'
DEFAULT_FROM_EMAIL = '[email protected]'
MANAGERS = ADMINS

DATABASES = {
'default': {
    'ENGINE' : 'django.db.backends.mysql',   
    'NAME' : '###',
    'USER' : '###',
    'PASSWORD' : '###',
    'HOST' : '/Applications/MAMP/tmp/mysql/mysql.sock',
}
}

TIME_ZONE = 'America/Tijuana'


LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = True

USE_L10N = True

MEDIA_DEBUG_DOC_ROOT = '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/media'
MEDIA_ROOT = '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/media'

MEDIA_URL = '/static_files/'

ADMIN_MEDIA_PREFIX = '/media/admin/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '####'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'cms.middleware.page.CurrentPageMiddleware', 
    'cms.middleware.user.CurrentUserMiddleware', 
    'cms.middleware.toolbar.ToolbarMiddleware',
    'cms.middleware.media.PlaceholderMediaMiddleware',
    #'django.middleware.cache.FetchFromCacheMiddleware',
    #'debug_toolbar.middleware.DebugToolbarMiddleware',
)

ROOT_URLCONF = 'zeiss_elearning.urls'

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth', 
    'django.core.context_processors.i18n', 
    'django.core.context_processors.request', 
    'django.core.context_processors.media', 
    'cms.context_processors.media',
)

TEMPLATE_DIRS = (,
    '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/templates',
    '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/cms/templates',
)

SESSION_COOKIE_AGE = 86400

LOGIN_URL = '/membership/login/'

LOGIN_REDIRECT_URL = "/"

AUTHENTICATION_BACKENDS = ( 
    'zeiss_elearning.shared.email_auth.EmailBackend',
    'django.contrib.auth.backends.ModelBackend',
)
AUTH_PROFILE_MODULE = 'membership.UserProfile'
FORCE_SCRIPT_NAME = ''

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'cms',
    'cms.plugins.text',
    'cms.plugins.picture',
    'cms.plugins.link',
    'cms.plugins.file',
    'cms.plugins.snippet',
    'cms.plugins.googlemap',
    'cms.plugins.zeiss_video',
    'cms.plugins.html',
    'cms.plugins.quiz',
    'cms.plugins.popup',
    'mptt',
    'publisher',
    'zeiss_elearning.forms',
    'zeiss_elearning.membership',
    'zeiss_elearning.quiz',
    'menus',
    'south',
)
INTERNAL_IPS = ('127.0.0.1',)

#CMS Settings


CMS_REDIRECTS = True
CMS_MENU_TITLE_OVERWRITE = True
CMS_DBGETTEXT = False

CMS_DEFAULT_TEMPLATE = 'base.html'
CMS_ALLOW_HTML_TITLES = False

CMS_TEMPLATES = (
    ('base.html', _('Default')),
    ('cirrus.html', _('Cirrus')),
    ('atlas.html', _('Atlas')),
)

# Site title for your template
CMS_SITE_TITLE = 'Zeiss Cirrus'

CMS_LANGUAGE_REDIRECT = False

CMS_LANGUAGES = (
    ('en', gettext('English')),
)
LANGUAGES = (
    ('en', gettext('English')),
)
CMS_APPLICATIONS_URLS = (
('zeiss_elearning.quiz.urls', 'Quiz')
)

urls.py

from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings

admin.autodiscover()

urlpatterns = patterns('',
    (r'^membership/', include('zeiss_elearning.membership.urls')),
    (r'^admin/', include(admin.site.urls)),

)
urlpatterns += patterns('',
    url(r'^', include('cms.urls')),
)
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static_files/(?P<path>.*)
, 'django.views.static.serve',
        {'document_root': settings.MEDIA_DEBUG_DOC_ROOT}),)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-10-27 13:06:02

根据提供的数据,这里的底线似乎是您的浏览器正在缓存媒体文件。解决此问题的推荐方法是在浏览器中超级刷新页面。查看您帖子上的所有评论。

但是,如果您确实不希望缓存媒体文件,您只需将它们设置为唯一的名称即可。就像这样。

 <link rel="stylesheet" type="text/css" href="/site_media/css/style.css?{% now "U" %}" />

现在,每次重新加载页面时,文件名都会根据 unix 时间戳略有不同,从而迫使浏览器始终重新加载它。

The bottom line here, based on the data provided, seems to be that your browser is caching the media files. The recommended method to resolve this is super refreshing the pages in your browser. See ALL the comments on your post.

However, If you really do not want the media files to be cached you can simply set them constantly unique names. Like so.

 <link rel="stylesheet" type="text/css" href="/site_media/css/style.css?{% now "U" %}" />

Now every time the page is reloaded the filename will be a little bit different based on the unix timestamp, forcing the browser to reload it all the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文