Django 1.6:如何在视图中访问静态文件 [英] Django 1.6: How to access static files in view

查看:410
本文介绍了Django 1.6:如何在视图中访问静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此处尝试过该解决方案,并且它对我不起作用。我正在基于Heroku的Python入门项目创建一个项目。

I've already tried the solution here and it didn't work for me. I'm creating a project based off the Heroku "Getting Started" project for Python.

views.py ,我希望能够访问 static / data / 文件夹中的文件。但是,我大多数尝试为文件创建正确的url都失败了。唯一有效的方法是将文件的绝对路径放入本地文件系统中,而这在我部署应用程序时显然将不起作用。

In views.py, I'd like to be able to access a file in the static/data/ folder. However, most of my attempts I make to create the correct url to the file have failed. The only thing that works is putting the absolute path to the file as it exists on my local file system, which obviously won't work when I deploy my app.

上一个尝试打开文件的尝试包括:

Previous attempts to open the file include:

from django.templatetags.static import static
url = static('data/foobar.csv')
os.path.isfile(url) # False

from django.conf import settings
url = os.path.join(settings.STATIC_URL, 'data/foobar.csv')
os.path.isfile(url) # False

这是我的目录结构:

/appname
  /app
    /templates
    views.py
  /appname
    /static
      /js
      /css
      /data
    settings.py
    urls.py

settings.py:

settings.py:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'appname.urls'

WSGI_APPLICATION = 'appname.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

DATABASES['default'] =  dj_database_url.config()

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)


推荐答案

而不是加入 STATIC_ROOT 和文件名,请使用 staticfiles_storage 接口。

Instead of joining the STATIC_ROOT with the filename, use the staticfiles_storage interface instead. This will also work with remote static files like S3/django-storages.

from django.contrib.staticfiles.storage import staticfiles_storage

url = staticfiles_storage.url('data/foobar.csv')

使用 staticfiles_storage ,您还可以执行简单的文件操作,例如打开,删除和保存。

With staticfiles_storage you can also do simple file operations like open, delete, save.

这篇关于Django 1.6:如何在视图中访问静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆