【问题标题】:Get the file path for a static file in django code在 django 代码中获取静态文件的文件路径
【发布时间】:2015-05-25 01:28:06
【问题描述】:

我有一个 django 应用程序,它发送包含图像文件作为 MIME 附件的电子邮件。电子邮件是从views.py 发送的,但为了附加文件,我需要获取图像的完整路径名,以便python 可以打开它们。这些文件位于我的应用程序的静态文件夹中,但我似乎无法找到一种方法来获取文件系统上文件的完整路径在开发模式下工作 - 它工作正常在收集静态后在生产中,但在开发中,它无法找到该文件,因为静态文件是从开发中的各个应用程序文件夹中提供的。

【问题讨论】:

    标签: python django


    【解决方案1】:

    使用finders-module 中的Django

    from django.contrib.staticfiles import finders
    
    result = finders.find('css/base.css')
    searched_locations = finders.searched_locations
    

    字符串result是文件系统路径,如果找不到,请仔细检查searched_locations

    【讨论】:

    • 谢谢。这应该是公认的答案,因为 OP 要求的是文件系统路径而不是 URL。
    • @SergeRogatch,在生产中,静态文件由 apache/nginx 提供,所以这种方法不会像调试模型那样工作。
    • 这对于确定 Django 从哪里加载静态文件非常有帮助,因此我可以将其他文件放在正确的位置。我刚刚使用了finders.find('.'),就成功了。谢谢!。
    【解决方案2】:

    project_dir.url添加到文件末尾

    if DEBUG:
        urlpatterns += patterns(
            '',
            url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
                {'document_root': MEDIA_ROOT}),
    
            url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
                {'document_root': STATIC_ROOT}),
    )
    

    project_dir.settings

    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    )
    STATIC_ROOT = os.path.join(BASE_DIR, 'static_debug')
    STATIC_URL = '/static/'
    
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    MEDIA_URL = '/media/'
    

    制作目录 media & static_debug(将它们添加到 .gitignore)

    project_dir/
        static/
            image.jpg
        settings.py
        urls.py
    apps/
        __init__.py
        some_app/
            static/
                some_app/
                    css/
                        style.css
    media/
    static_debug/
    

    现在您可以运行项目或直接运行

    python manage.py collectstatic
    

    从视图访问

    from django.templatetags.static import static
    
    static('image.jpg')
    static('some_app/css/style.css')
    

    从模板访问

    {% load staticfiles %}
    
    {% static 'image.jpg' %}
    {% static 'some_app/css/style.css' %}
    

    【讨论】:

    • 在视图中,这会返回一个相对 URI(例如注入 html),而不是用于open()的文件系统路径
    【解决方案3】:

    既然你想要一个应用的静态,这就是你所需要的!

    from django.contrib.staticfiles import finders
    
    APP_LABEL = 'app_youre_looking_at'
    FILE_NAME = 'file_name_you_want_to_read_or_write.ext'
    
    stores = finders.AppDirectoriesFinder(app_names={APP_LABEL}).storages
    print(f'Here it is: {stores[APP_LABEL].path(FILE_NAME)}')
    

    FILE_NAME 不需要存在。您可以使用stores[APP_LABEL].path('') 仅获取路径。

    【讨论】:

      【解决方案4】:

      在犯了很多错误之后,以下事情对我有用..

      假设您有以下目录结构(感谢@madzohan

      project_dir/
          static/
              image.jpg
          settings.py
          urls.py
      apps/
          __init__.py
          some_app/
              static/
                  some_app/
                      css/
                          style.css
      

      现在如果你想获取 project_dir/static/image.py 的路径,那么

      在您的 project_dir/settings.py 文件中,在文件范围内定义一个 STATIC_DIR 变量,例如。 (如果之前没有做过)

      project_dir/settings.py

      import os
      
      # after your other file variables
      STATIC_DIR = os.path.join(BASE_DIR, 'static')
      

      现在在您要访问 /static/image.jpg 文件的 app_name/views.py 文件中

      app_name/views.py

      import os
      from project_dir.settings import STATIC_DIR
      
      # here I want the /static/image.jpg file then
      image = os.path.join(STATIC_DIR, 'image.jpg')
      

      然后您可以通过图像变量访问 image.jpg 文件

      希望这对你有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-26
        • 1970-01-01
        • 2020-08-19
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多