首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查看静态文件

查看静态文件
EN

Stack Overflow用户
提问于 2016-01-02 09:52:27
回答 2查看 3.9K关注 0票数 2

为了集成DjangoEmber,我决定在Django视图中为Ember SPA提供服务(避免CORS问题,只有一个用于前端和API的服务器,等等)。我这样做:

代码语言:javascript
复制
# urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api/', include(api_urls, namespace='api')),
    ...
    url(r'^$', views.emberapp, name='emberapp'),
    ...
]

# views.py

from django.http import HttpResponse


def emberapp(request):
    # The Ember frontend SPA index file
    # This only works in development, and is anyway hacky
    EMBER_FE_INDEX_HTML = '/absolute/path/to/my/frontend/static/fe-dist/index.html'
    template_file = open(EMBER_FE_INDEX_HTML)
    html_content = index_file.read()
    index_file.close()
    return HttpResponse(html_content)

index.html是静态资产的一部分。在开发过程中,这非常容易:

  • index.html可由文件系统中的Django应用程序直接访问。
  • 我知道索引文件的绝对路径。

但是在生产过程中,事情要复杂得多,因为静态资产不是django应用程序的本地资产,而是在亚马逊S3上可以访问的。我用django-storages来做这个。

无论使用什么后端存储/服务静态文件,我如何从视图中读取静态文件的内容?

EN

回答 2

Stack Overflow用户

发布于 2016-01-02 11:58:25

首先,我不认为你这样做是个好主意。

但是,要回答您的问题:在您的settings.py中,您可能已经定义了Django将收集所有静态文件的目录。

代码语言:javascript
复制
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

因此,在您的视图中,只需获取文件os.path.join(settings.STATIC_ROOT, 'index.html')

也就是说,您应该通过you服务器提供index.html,与静态/文件、robots.txt、favicon.ico等相同,而不是通过Django。view服务器要快得多,使用适当的缓存,它在Nginx或Apache设置中只有一行,而不是Django中的整个视图函数。

票数 1
EN

Stack Overflow用户

发布于 2016-01-02 12:06:43

这是我目前的解决方案。在开发中工作,还不确定生产(您需要提交未经测试的代码来验证Heroku中与生产相关的代码)。

代码语言:javascript
复制
from django.conf import settings
from django.http import HttpResponse
from django.core.files.storage import get_storage_class

FE_INDEX_HTML = 'fe/index.html'  # relative to the collectstatic directory


def emberapp(request):
    # The Ember frontend SPA index file
    # By getting the storage_class like this, we guarantee that this will work
    #   no matter what backend is used for serving static files
    #   Which means, this will work both in development and production
    #   Make sure to run collectstatic (even in development)
    # TODO: how to use this in development without being forced to run collectstatic?
    storage_class = get_storage_class(settings.STATICFILES_STORAGE)
    # TODO: reading from a storage backend can be slow if assets are in a third-party server (like Amazon S3)
    #   Maybe streaming the static file from the server would be faster?
    #   No redirect to the Amazon S3 asset, please, since the Ember App needs to
    #   run from the same URL as the API, otherwise you get CORS issues
    with storage_class().open(FE_VWORKS_INDEX_HTML) as index_file:
        html_content = index_file.read()
    return HttpResponse(html_content)

或者,使用StreamingHttpResponse进行答复,这不会强迫Django在内存中读取整个文件(并等待读取):

代码语言:javascript
复制
def emberapp(request):
    # The Ember frontend SPA index file
    # By getting the storage_class like this, we guarantee that this will work
    #   no matter what backend is used for serving static files
    #   Which means, this will work both in development and production
    #   Make sure to run collectstatic (even in development)
    # TODO: how to use this in development without being forced to run collectstatic?
    storage_class = get_storage_class(settings.STATICFILES_STORAGE)
    index_file = storage_class().open(FE_INDEX_HTML)
    return StreamingHttpResponse(index_file)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34564579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档