Django 静态文件开发 [英] Django Static Files Development

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

问题描述

从有关该主题的类似标题的数量来看,这似乎是造成很多混乱的根源,但是尝试使用 django 开发服务器在静态文件上找到的所有东西我几乎放弃了希望!

This seems to be a source of much confusion judging by the amount of similar titles on the subject, however trying everything I could find on static files with the django development server I've almost given up hope!

所以我的静态文件是从 C:/Users/Dan/seminarWebsite/static/ 提供的,其中我有用于图像、CSS 等的子文件夹.

So my static files are served from C:/Users/Dan/seminarWebsite/static/, in which I have sub folders for images, css etc.

设置:

STATIC_ROOT = 'C:/Users/Dan/seminarWebsite/static/'  
STATIC_URL = '/static/'  

静态文件应用程序也处于活动状态.

The static files app is also active.

网址:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns  
urlpatterns += staticfiles_urlpatterns()

模板:

"{{ STATIC_URL }}images/vision.jpeg"

但是只有一个断开的链接出现在这个地址:http://127.0.0.1:8000/homepage/images/vision.jpeg 我认为它不应该在那个地址(主页是静态图像文件被调用到的页面的 url 名称).

However only a broken link appears and at this address: http://127.0.0.1:8000/homepage/images/vision.jpeg and I don't think it should be at that address (homepage is the url name of the page the static image file is being called to).

推荐答案

根据您目前发布的内容,您似乎正在关注 django.contrib.staticfiles.我同意 文档 可以很难理解,特别是如果你是 Django 的新手.

Based on what you've posted so far, it looks like you're following the docs for django.contrib.staticfiles. I agree that the docs can be difficult to follow especially if one is new to django.

我相信混淆源于 django.contrib.staticfiles两种操作模式:

I believe the confusion stems from the fact that django.contrib.staticfiles has two modes of operation:

  1. 在开发阶段,开发服务器<使用/a>,它动态搜索预定义目录中的静态文件,并使其在 STATIC_URL
  2. 上可用
  3. 对于部署,它有助于将静态文件整理到单个目录(使用 STATIC_ROOT 定义),以便可以使用适合静态文件的网络服务器托管静态文件.此整理是使用 python ./manage.py collectstatic 完成的.
  1. During the development phase where the development server is used, it dynamically searches for static files in predefined directories and make it available on STATIC_URL
  2. For deployment, it assists in collating static files to a single directory (defined using STATIC_ROOT) so that the static files can be hosted using a webserver suited for static files. This collation is done using python ./manage.py collectstatic.

这里简要介绍了如何启动和运行.我没有机会尝试,所以可能会有错误.希望这可以帮助您入门,至少可以帮助您理解文档.如有疑问,请参阅文档.

Here's a quick summary of how to get up and running. I haven't had a chance to try it out so there may be mistakes. Hopefully this will help you get started and at the very least help you understand the docs. When in doubt, do refer to the docs.

  1. 确保INSTALLED_APPS

指定STATIC_URL.这将是您的静态文件所在的路径.

Specify STATIC_URL. This will be the path where your static files will be hosted on.

STATIC_URL = '/static/'

  • 确保您的文件位于正确的目录中.默认情况下,staticfiles 将在每个已安装应用程序的 static/ 目录以及 STATICFILES_DIRS.(此行为取决于 STATICFILES_FINDERS).在您的情况下,您可能希望在 STATICFILES_DIRS 中指定您的目录:

  • Make sure your files are in the correct directories. By default, staticfiles will look for files within the static/ directory of each installed app, as well as in directories defined in STATICFILES_DIRS. (This behaviour depends on backends listed in STATICFILES_FINDERS). In your case, you'd probably want to specify your directory in STATICFILES_DIRS:

    STATICFILES_DIRS = ( 
          'C:/Users/Dan/seminarWebsite/static/',  
    )
    

  • 通过将以下内容添加到 urls.pyend 使视图可访问:

  • Make the view accessible by adding the following to the end of urls.py:

    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    urlpatterns += staticfiles_urlpatterns()
    

  • 确保settings.py 中有DEBUG = True.

    就是这样.

    如果你运行你的开发服务器 (./manage.py runserver),你应该能够通过 http://localhost:8000/static/images/vision 访问你的文件.jpeg(服务于 C:/Users/Dan/seminarWebsite/static/images/vision/jpeg).

    If you run your dev server (./manage.py runserver), you should be able to access your file via http://localhost:8000/static/images/vision.jpeg (which serves C:/Users/Dan/seminarWebsite/static/images/vision/jpeg).

    有两种方法可以获得静态文件的正确链接 - 使用 staticfiles 模板标签,并使您的模板可以访问 STATIC_URL.既然您尝试了后者,我们将坚持下去.

    There are two ways to get a correct link for your static files - using the staticfiles template tag, and making STATIC_URL accessible to your templates. Since you've attempted the latter, we'll stick to that.

    1. 确保您在 TEMPLATE_CONTEXT_PROCESSORS 中有 'django.core.context_processors.static'.如果你还没有重新定义 TEMPLATE_CONTEXT_PROCESSORS 那么没什么可做的,因为 它应该在默认情况下.

    1. Make sure you have 'django.core.context_processors.static' in TEMPLATE_CONTEXT_PROCESSORS. If you haven't redefined TEMPLATE_CONTEXT_PROCESSORS then there is nothing to do since it should be there by default.

    确保您使用 RequestContext 渲染模板时.示例:

    Make sure you use RequestContext when rendering your template. Example:

    from django.template import RequestContext
    # ...
    
    def some_view(request):
        # ...
        return render_to_response('my_template.html', {
            "foo" : "bar",  # other context 
        }, context_instance = RequestContext(request))
    

  • 您现在应该可以在 my_template.html 中使用以下内容:

    You should now be able to use the following in your my_template.html:

    <a href="{{ STATIC_URL }}images/vision.jpeg" />
    

    在生产服务器上托管静态文件.

    如果您需要提供的所有静态文件都存储在同一个目录 (C:/Users/Dan/seminarWebsite/static) 中,那么您就快完成了.简单地将您的网络服务器配置为在 /static/(或您将 STATIC_URL 设置为的任何内容)上托管该目录,您就可以开始了.

    Hosting static files on production server.

    If all the static files you need to serve are store in that one directory (C:/Users/Dan/seminarWebsite/static), then you're almost there. Simple configure your webserver to host that directory on /static/ (or whatever you set STATIC_URL to) and you're good to go.

    如果您的文件分散在不同的目录和/或特定于应用程序的静态文件中,那么您需要整理它们.

    If you have files scattered in different directories and/or app specific static files, then you'll need to collate them.

    1. 设置STATIC_ROOT 到要存储整理文件的目录.

    1. Set STATIC_ROOT to the directory where you want to store the collated files.

    运行./manage.py collectstatic 进行整理.

    Run ./manage.py collectstatic to do the collation.

    将您的网络服务器配置为在 /static/(或您将 STATIC_URL 设置为的任何内容)上托管该目录 (STATIC_ROOT).

    Configure your webserver to host the that directory (STATIC_ROOT) on /static/ (or whatever you set STATIC_URL to).

    这篇关于Django 静态文件开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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