【问题标题】:Django get the static files URL in viewDjango 获取视图中的静态文件 URL
【发布时间】:2012-07-28 03:21:20
【问题描述】:

我正在使用 reportlab pdfgen 创建 PDF。在 PDF 中有一个由drawImage 创建的图像。为此,我需要图像的 URL 或视图中图像的路径。我设法构建了 URL,但是如何获取图像的本地路径?

我如何获得网址:

prefix = 'https://' if request.is_secure() else 'http://'
image_url = prefix + request.get_host() + STATIC_URL + "images/logo_80.png"

【问题讨论】:

    标签: django django-views django-staticfiles


    【解决方案1】:
    # Older Django <3.0 (also deprecated in 2.0):
    from django.contrib.staticfiles.templatetags.staticfiles import static
    
    # Django 3.0+
    from django.templatetags.static import static
    
    url = static('x.jpg')
    

    url 现在包含'/static/x.jpg',假设静态路径为'/static/'

    【讨论】:

    • 您知道是否有一种干净的方式可以将主机名添加到静态 URL(如果 STATIC_URL 中不存在)?我需要在邮件中添加图像或其他资源,用户将无法找到具有相关 url 的资源。
    • 这对我在 Debug 中运行时不起作用(尚未尝试使用 DEBUG=False)。我只是将路径传递给返回的静态方法。使用 Django 1.6。有什么想法吗?
    • 对于与@Shawn(或我)有相同问题的任何人,这可能是因为您提供的路径以斜杠开头。不要使用static('/style.css'),而是使用static('style.css')
    • 在 Django 2.0 中,这将显示弃用通知。请改用from django.templatetags.static import static
    • @Anupam 您可能将'/static' 添加到static 函数的参数中,这是不对的。试试static('my_file_name.xlsx')
    【解决方案2】:

    编辑:如果您使用的是 Django >=3.0,请参阅 Django get the static files URL in view。 Django 1.X 版本回答了这个问题。

    dyve 的回答很好,但是,如果您在 django 项目中使用“缓存存储”并且静态文件的最终 url 路径应该得到“散列”(例如 style.aaddd9d8d8d7.css em> 来自 style.css),那么您无法使用 django.templatetags.static.static() 获得精确的 url。相反,您必须使用来自django.contrib.staticfiles 的模板标签来获取散列网址。

    另外,在使用开发服务器的情况下,此模板标记方法返回非散列的url,因此无论主机是开发还是生产,您都可以使用此代码! :)

    from django.contrib.staticfiles.templatetags.staticfiles import static
    
    # 'css/style.css' file should exist in static path. otherwise, error will occur 
    url = static('css/style.css')
    

    【讨论】:

    • 谢谢你...我花了一段时间才弄清楚为什么我没有注入我的 md5 哈希
    • 这个答案仍然受到欢迎并且被积极使用,所以我改进了我接受的答案,并归功于@Kenial。这仍然是该问题的首选解决方案。
    • 在 Django 3.2 中,django.contrib.staticfiles 中没有模板标签。见stackoverflow.com/a/59355195
    • @dfrankow 似乎我在 Django 1.3 的某个地方做出了这个答案......哇已经有一段时间了。感谢您改进它!
    【解决方案3】:

    这是另一种方式! (在 Django 1.6 上测试)

    from django.contrib.staticfiles.storage import staticfiles_storage
    staticfiles_storage.url(path)
    

    【讨论】:

    • 很好的解决方案,因为如果 DEBUG 设置为 False,这将返回散列 URL。可以选择强制哈希 URL,如下所示:staticfiles_storage.url(path, force=True)
    【解决方案4】:

    @dyve 的回答在开发服务器中对我不起作用。相反,我用find 解决了它。这是函数:

    from django.conf import settings
    from django.contrib.staticfiles.finders import find
    from django.templatetags.static import static
    
    def get_static(path):
        if settings.DEBUG:
            return find(path)
        else:
            return static(path)
    

    【讨论】:

      【解决方案5】:

      如果要获取绝对url(包括协议、主机和端口),可以使用request.build_absolute_uri函数,如下所示:

      from django.contrib.staticfiles.storage import staticfiles_storage
      self.request.build_absolute_uri(staticfiles_storage.url('my-static-image.png'))
      # 'http://localhost:8000/static/my-static-image.png'
      

      【讨论】:

        【解决方案6】:

        使用默认的static标签:

        from django.templatetags.static import static
        static('favicon.ico')
        

        django.contrib.staticfiles.templatetags.staticfiles 中有另一个标签(在接受的答案中),但在 Django 2.0+ 中已弃用。

        【讨论】:

          【解决方案7】:

          从 Django 3.0 你应该使用from django.templatetags.static import static:

          from django.templatetags.static import static
          
          ...
          
          img_url = static('images/logo_80.png')
          

          【讨论】:

            【解决方案8】:

            简而言之,你需要得到

            • STATIC_URL
            • STATIC_ROOT
            • urlpatterns
            • staticfiles
            • templatetags
            • url parameters

            一切都在正确的位置,以使其正常工作。此外,在实时部署中,情况会有所不同,您花费 3 个小时制定的当前设置很可能在您的本地机器上运行,但在服务器上运行。

            所以我采用了传统的方式!!

            app
            ├── static
            │   └── json
            │       └── data.json
            └── views.py
            

            views.py

            import os
            
            with open(os.path.abspath(os.getcwd()) + '/app/static/json/data.json', 'r') as f:
                pass
            

            【讨论】:

              猜你喜欢
              • 2018-10-20
              • 2019-03-05
              • 2014-04-11
              • 1970-01-01
              • 1970-01-01
              • 2012-07-14
              • 2014-11-23
              • 2013-12-14
              • 2018-11-29
              相关资源
              最近更新 更多