Django 静态文件开发

发布于 2025-01-02 22:42:38 字数 705 浏览 0 评论 0 原文

从该主题上类似标题的数量来看,这似乎是一个令人困惑的来源,但是尝试了我在 django 开发服务器上在静态文件上能找到的所有内容,我几乎放弃了希望!

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

设置:

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< /code> 我认为它不应该位于该地址(主页是调用静态图像文件的页面的 URL 名称)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2025-01-09 22:42:38

根据您迄今为止发布的内容,您似乎正在遵循 django.contrib.staticfiles。我同意文档可能很难理解,尤其是对于 django 新手来说。

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

  1. 在开发阶段,开发服务器,它动态搜索预定义目录中的静态文件并使其可用于STATIC_URL
  2. 对于部署,它有助于将静态文件整理到单个目录(使用STATIC_ROOT 定义),以便可以使用适合静态文件的网络服务器托管静态文件。此排序规则是使用 python ./manage.pycollectstatic 完成的。

以下是有关如何启动和运行的快速摘要。我还没有机会尝试,所以可能会有错误。希望这将帮助您入门,并至少帮助您理解文档。如有疑问,请参阅文档。

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

  1. 确保 INSTALLED_APPS 中有 'django.contrib.staticfiles'

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

    <前><代码> STATIC_URL = '/静态/'

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


    <前><代码> STATICFILES_DIRS = (
    'C:/Users/Dan/seminarWebsite/static/',

  4. 通过将以下内容添加到 url 的末尾,使视图可访问。 py

     from django.contrib.staticfiles.urls import staticfiles_urlpatterns
     urlpatterns += staticfiles_urlpatterns()
    
  5. 确保 DEBUG = True 中>settings.py.

就是这样。

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

使用模板标签链接到模板中的静态文件

在 Django 1.10 之前,您可以使用以下内容:

{% load staticfiles %}
<a href="{ static 'images/vision.jpeg' %}" />

从 Django 1.10 及更高版本,您应该使用:

{% load static %}
<a href="{ static 'images/vision.jpeg' %}" />

使用 STATIC_URL 链接到模板中的静态文件

有两种方法可以为您的静态文件获取正确的链接文件 - 使用 静态文件模板标记,如上所述,这是首选方法,并使您的模板可以访问 STATIC_URL。既然您已经尝试过后者,那么这里是如何做到这一点的。

  1. 如果使用 Django 版本低于 1.10,请确保您有 'django.core.context_processors.static''django.template.context_processors.static' 如果使用版本从 TEMPLATE_CONTEXT_PROCESSORS 1.10 开始。如果您还没有重新定义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< /a> 到要存储整理文件的目录。


  2. 运行 ./manage。 pycollectstatic 进行排序。

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

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 template using template tags

Before Django 1.10 you can use the following:

{% load staticfiles %}
<a href="{ static 'images/vision.jpeg' %}" />

From Django 1.10 and later you should use:

{% load static %}
<a href="{ static 'images/vision.jpeg' %}" />

Linking to static files in your templates by using STATIC_URL

There are two ways to get a correct link for your static files - using the staticfiles template tag as described above which is the preferred method, and making STATIC_URL accessible to your templates. Since you've attempted the latter, here's how to do that.

  1. Make sure you have 'django.core.context_processors.static' if using Django version less than 1.10 or 'django.template.context_processors.static' if using versions from 1.10 onwards 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).

极致的悲 2025-01-09 22:42:38

我想分享我对当地发展的看法。它与 Shawn Chin 的答案类似,但不需要 DEBUG=True 和特殊的 urlpatterns,我认为这更通用和干净。

第 1、2、3 点与答案中的相同,但以下内容有所不同:

  1. 对所有环境以相同方式提供 URL:
# main urls.py

urlpatterns = (
    [
        # URLs from apps are here
        ...
    ]
    + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
)

与 Django 4.2 相关

I'd like to share my reciept for a local development. It's similar to Shawn Chin's answer, but not required DEBUG=True and special urlpatterns, which is more universal and clean as I think.

Points 1, 2, 3 are the same as in the answer, but the following are different:

  1. Serve URLs same way for all environments:
# main urls.py

urlpatterns = (
    [
        # URLs from apps are here
        ...
    ]
    + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
)

Relevant for Django 4.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文