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

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

问题描述

这似乎是一个很大的困惑的来源,根据相关标题的数量来判断,但是尝试使用django开发服务器在静态文件中找到的所有东西我已经放弃了希望!



所以我的静态文件是从C:/ Users / Dan / seminarWebsite / static / 提供的,其中有我的图像,css等子文件夹。 p>

设置:

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

静态文件应用程序也是活动的。



URLS:

  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名称,静态图像文件被调用)。

解决方案

根据您目前发布的内容,您似乎正在关注 django.contrib.staticfiles 。我同意文档可以很难跟随,特别是如果一个是新的django。



我相信这个混乱是因为 django.contrib.staticfiles 两个操作模式:


  1. 在开发阶段,开发服务器,它会动态搜索预定义目录中的静态文件,并使其在 STATIC_URL

  2. 对于部署,它有助于将静态文件整理到单个目录(使用 STATIC_ROOT 定义)以便静态文件可以使用适合静态文件的网络服务器托管。这个排序规则使用 python ./manage.py collectstatic 完成。

这是快速总结如何起床和运行。我没有机会尝试,所以可能会有错误。希望这将有助于您入门,至少帮助您了解文档。如有疑问,请参考文档。



在开发服务器上托管静态文件




  1. 确保你'django.contrib.staticfiles' INSTALLED_APPS


  2. 指定 STATIC_URL 。这将是您的静态文件将被托管的路径。

      STATIC_URL ='/ static /'


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

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


  4. 通过向 urls.py 结尾添加以下内容, :$ /

    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / code>


  5. 确保您的 DEBUG = True settings.py


就是这样。



如果您运行您的开发人员服务器( ./ manage.py runserver ),您应该可以访问文件通过 http:// localhost:8000 / static / images / vision.jpeg (其中 C:/ Users / Dan / seminarWebsite / static /



链接到模板中的静态文件



有两种方法可以获取静态文件的正确链接 - 使用 staticfiles模板标签,并使您的模板可以访问 STATIC_URL 。因为你尝试过后者,我们会坚持下去。


  1. 确保你有 TEMPLATE_CONTEXT_PROCESSORS 中的'django.core.context_processors.static'。如果您没有重新定义 TEMPLATE_CONTEXT_PROCESSORS 然后没有任何作用,因为它应该在默认情况下


  2. 确保在呈现模板时使用 RequestContext 。示例:

      from django.template import RequestContext 
    #...

    def some_view (请求):
    #...
    返回render_to_response('my_template.html',{
    foo:bar,#其他上下文
    },context_instance = RequestContext (请求))


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

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



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



如果您需要提供的所有静态文件都存储在该目录中( C:/ Users / Dan / seminarWebsite / static ),那么你几乎在那里。简单地配置您的网络服务器来承载该目录 / static / (或您设置的任何内容 STATIC_URL 至)



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


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


  2. 运行 。 /management.py collectstatic 来进行整理。


  3. 配置您的网络服务器来托管该目录(<$ / static / (或您设置的任何内容 STATIC_URL )中的c $ c> STATIC_ROOT )。



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!

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

SETTINGS:

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

The static files app is also active.

URLS:

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

TEMPLATE:

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

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).

解决方案

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.

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

  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.

Hosting static files on the development server

  1. Make sure you have 'django.contrib.staticfiles' in INSTALLED_APPS

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

    STATIC_URL = '/static/'
    

  3. 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/',  
    )
    

  4. 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()
    

  5. Make sure you have DEBUG = True in settings.py.

That's it.

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).

Linking to static files in your templates

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. 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.

  2. 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))
    

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

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

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. Set STATIC_ROOT to the directory where you want to store the collated files.

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

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

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

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