Thursday, March 31, 2011

Re: Serve a tracking pixel via Django

On Fri, Apr 1, 2011 at 8:09 AM, Adam Nelson <adam@varud.com> wrote:
> We have one static file that needs to be served after business logic has run
> (i.e. log a view on a specific url based on a unique code).  We used to use
> serve() in production.  This has always been frowned upon but with Django
> 1.3, the errors are worse.

Errors or warnings? There's a big difference. Errors mean it isn't
working. Warnings mean it is working, but something is telling you to
pay attention to something.

> Can one return an image in any other way?

Sure. Write a view that returns the content. It's 3 lines of code (a
couple more if you count imports and whitespace). Added bonus -- it's
actually more efficient than serve(), because it doesn't impose a disk
access overhead every time it gets called.

TRANSPARENT_1_PIXEL_GIF =
"\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"

def tracking_pixel(request):
return HttpResponse(TRANSPARENT_1_PIXEL_GIF, content_type='image/gif')

Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment