新的Django App MEDIA_URL路径不正确

时间:2014-10-17 17:02:14

标签: python django

所以,我已经通过python manage.py startapp foo

在Django中创建了一个新应用

我的新应用不会通过/site_media/加载{{ MEDIA_URL }}目录中的任何文件。他们正在尝试从App的目录路径,而不是/site_media/目录。

<小时/> 示例: 而不是从http://sitename/site_media/bootstrap/bootstrap.min.css

加载

它尝试从http://sitename/foo/bootstrap/bootstrap.min.css

加载

以下是定义settings.py

MEDIA_URL的摘录
MEDIA_URL = '/site_media/'

我可以通过将{{ MEDIA_URL }}替换为/site_media/和我的base.html中的show_foo.html来强制在应用中正确加载文件,但这会破坏路径上的路径网站的其余部分。

我不确定有什么人愿意尝试去诊断这个问题,但我很难过!

<小时/> 以防万一: 来自我的app目录中的urls.py

#!/usr/bin/python
# -*- coding: UTF8 -*-
from django.conf.urls.defaults import *

urlpatterns = patterns('foo_web.foo_track.views',   
url('^$','view_foo_track',name='foo_home'),  
url('^newentry/(?P<entry_id>\d+)$','write_form_data',name='foo_track_new'),
)

<小时/> settings.py编辑*删除了评论的可读性

import os
current_dir = os.path.abspath(os.path.dirname(__file__))
from os import sys, path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
import run_server

cherry_config,django_config = run_server.get_web_server_parameters()
DEBUG = django_config['DEBUG']
TEMPLATE_DEBUG = django_config['TEMPLATE_DEBUG']
CACHE_MODE = django_config['CACHE_MODE']
DB = django_config['DB']
HOST = django_config['HOST']


LOGIN_URL = '/'
LOGOUT_URL = '/'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': '%s' % DB,             
        'USER': 'postgres', 
        'PASSWORD': '*****',
        'HOST': '%s' % HOST,
        'PORT': '5432',              
    }
}

CACHES = {
    'default': {
        'BACKEND': CACHE_MODE,
        'LOCATION': 'my_cache_table',
        'TIMEOUT': 1800,
        'OPTIONS': {
            'MAX_ENTRIES': 10000
        }
    }
}

ADMINS = (
    ('**', '**'),
)

MANAGERS = ADMINS

#~ EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = '**' 
SERVER_EMAIL = '**' 
EMAIL_USE_TLS = True
EMAIL_HOST = "**"
EMAIL_HOST_USER = "**"
EMAIL_HOST_PASSWORD = "**"

TIME_ZONE = 'America/Chicago'

LANGUAGE_CODE = 'en-us'

#~ gettext = lambda s: s
#~ LANGUAGES = (
    #~ ('de', gettext('German')),
    #~ ('en', gettext('English')),
#~ )

SITE_ID = 1

USE_I18N = True

USE_L10N = True

USE_TZ = True

MEDIA_ROOT = current_dir + '/media/'

MEDIA_URL = '/site_media/'

STATIC_ROOT = current_dir + '/media/static/'

STATIC_URL = '/site_static/'

STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

SECRET_KEY = '***'

if DEBUG:
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    #     'django.template.loaders.eggs.Loader',
    )    
else:
    TEMPLATE_LOADERS = (
        ('django.template.loaders.cached.Loader', (
        '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',
    'sensei_web.middleware.FilterPersistMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    #~ 'sensei_web.middleware.ProfileMiddleware'

    #~ 'django.middleware.cache.UpdateCacheMiddleware',
    #~ 'django.middleware.common.CommonMiddleware',
    #~ 'django.middleware.cache.FetchFromCacheMiddleware',
)

ROOT_URLCONF = 'sensei_web.urls'

WSGI_APPLICATION = 'sensei_web.wsgi.application'

TEMPLATE_DIRS = (
    current_dir + '/templates',

)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
     'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'foo.foo',
    'foo.foo',
    #~ 'foo.foo',
    'foo.foo',
    'foo.foo',
    'foo',
   'foo.foo',
   'foo.foo_track',
    )

ACCOUNT_ACTIVATION_DAYS = 7

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

2 个答案:

答案 0 :(得分:1)

感谢大家的帮助!事实证明这只是我做的一个错误:(虽然非常有趣的结果。

我在日志中注意到有关A (% csrf_token %) was used in a template, but the context did not provide the value. This is uaually caused by not using RequestContext.

的一些有趣内容

瞧,在我的views.py这行不正确: return render_to_response('foo_track/foo_track_show.html',{'access':access})

它应该有RequestContext(request)这样: return render_to_response('foo_track/foo_track_show.html',{'access':access},RequestContext(request))

现在一切正常。啧!

答案 1 :(得分:0)

我相信你必须明确定义一个&#34; site_media&#34;你的urls.py中的网址

像这样:

将此添加到导入

from django.conf import settings

这是你的urlpatterns:

url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT}))

否则,Django将假设您使用不同的Web服务器来提供静态内容。顺便说一下,这是一个好主意。这是关于serving static content with Apache and mod_wsgi

的文章