【问题标题】:django pixel trackingdjango 像素跟踪
【发布时间】:2011-10-30 03:43:46
【问题描述】:

我正在使用 django 对电子邮件进行像素跟踪

从 django 视图返回实际图像是否容易(以及如何完成?)还是只返回到实际图像所在的 url 的重定向更容易?

【问题讨论】:

    标签: django image tracking


    【解决方案1】:

    您不需要为跟踪器像素提供实际图像。事实上,最好没有。

    只需将视图用作图像标签的来源,并让它返回空白响应。

    【讨论】:

    • 我回应的内容类型重要吗?
    • 是的,你应该把它设为 image/png。
    • 作为记录,我注意到发送空白响应可能会出现问题 - 至少在 Chrome 上的 gmail 中,这会导致消息中出现损坏的图像字形。最好流一个琐碎的图像,as suggested by Russell Keith-Magee
    【解决方案2】:

    由于这是我的谷歌搜索的第一个结果,并且最佳答案隐藏在 Daniel 的链接中(但没有提到最好的),我想我会发布答案,所以没有人会试图返回空白回复正如迈克尔指出的那样,这并不理想。

    解决方案是使用标准视图并返回一个 HttpResponse,其中包含构成单个像素 gif 的原始数据。不必敲击磁盘或重定向是一个巨大的优势。

    请注意,url 模式使用跟踪代码作为图像名称,因此 url 中没有明显的 ?code=jf8992jf。

    from django.conf.urls import patterns, url
    from emails.views.pixel import PixelView
    
    urlpatterns = patterns('',
        url(r'^/p/(?P<pixel>\w+).gif$', PixelView.as_view(), name='email_pixel'),
    )
    

    这是视图。请注意,它使用 cache_control 来防止请求疯狂运行。例如,Firefox(以及许多电子邮件客户端)每次都会请求两次图像,出于某种您可能不关心但需要担心的原因。通过添加 max_age=60,您每分钟只会收到一个请求。

    from django.views.decorators.cache import cache_control
    from django.http.response import HttpResponse
    from django.views.generic import View   
    
    class PixelView(View):
    
        @cache_control(must_revalidate=True, max_age=60)
        def get(self, request, pixel):
            """
            Tracking pixel for opening an email
            :param request: WSGIRequest
            :param pixel: str
            :return: HttpResponse
            """
    
            # Do whatever tracking you want here
    
            # Render the pixel
            pixel_image = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
            return HttpResponse(pixel_image, content_type='image/gif')
    

    【讨论】:

    • 值得注意的是,在 Python3 中 pixel_image 应该是 prefixedb,所以它的类型是 bytes 而不是 str。否则,响应将不是实际图像,并且仍会导致 Michael 提到的损坏的图像字形。
    • 感谢 @alxs 的更新 - 我已更新示例代码以包含 b
    • 这个答案对我不起作用。相反,我在 gist.github.com/simonw/10235798 找到了一个修复程序,它提供了以下答案:PIXEL_GIF_DATA = base64.b64decode(b"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")
    【解决方案3】:

    Django 有一个static file 帮助器,可以用来提供图像,但由于性能原因不推荐使用。我相信拥有一个视图来记录跟踪像素,然后重定向到 serves the actual image via the webserver 的 URL 将为您提供最佳性能。

    【讨论】:

      【解决方案4】:

      Python2.x:

      from django.http import HttpResponse
      
      PIXEL_GIF_DATA = """
      R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
      """.strip().decode('base64')
      
      def pixel_gif(request):
          return HttpResponse(PIXEL_GIF_DATA, content_type='image/gif')
      

      Python3.x:

      import base64
      from django.http import HttpResponse
      
      PIXEL_GIF_DATA = base64.b64decode(
          b"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")
      
      def pixel_gif(request):
          return HttpResponse(PIXEL_GIF_DATA, content_type='image/gif')
      

      来源:https://gist.github.com/simonw/10235798

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-22
        • 2011-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多