我已经停止使用django 1.1创建新项目,从那时起,它只用于已经创建的应用程序.从那时起,我猜静态服务以某种方式改变了(项目正在使用Django 1.2.4).我挣扎了几个小时但没有结果,所以如果有人知道我做错了什么,请告诉我.
我的设置 :
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__)) MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media') MEDIA_URL = '/static/' ADMIN_MEDIA_PREFIX = '/media/' SECRET_KEY = '(d9bahjuyy_i-)4b(w9gc5a&s&5jemcn7&b^wrbuemah3p-6^#' TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', # 'django.template.loaders.eggs.Loader', ) MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ) TEMPLATE_DIRS = ( os.path.join(PROJECT_PATH, 'templates'), ) INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.admin', 'django.contrib.admindocs', 'project', ) TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.auth", "django.core.context_processors.request", "django.core.context_processors.media", "django.core.context_processors.csrf", "django.core.context_processors.i18n", )
网址:
urlpatterns += patterns('', (r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes' : True}), )
媒体文件位于../project_path/media中
在模板中,我将{{MEDIA_URL}}路径作为/ static /但我的文件未加载.转到http://127.0.0.1:8000/static/(有或没有上一个斜杠)显示根页面.Firebug显示页面的html代码代替javascript文件.我宁愿期待404错误.哪里出了问题?
突然变为1.3.问题仍然存在
http://www.megaupload.com/?d=2BGN8BU2
如果您使用的是render_to_response,则必须传递可选的第3个参数:
context_instance=RequestContext(request)
soe完整的代码看起来像:
return render_to_response('index.html',{'dict':'ionary'},context_instance=RequestContext(request))
{{STATIC_URL}}与RequestContext一起使用,而render_to_response的默认值是Context