Since this was the first result in my Google search, and the best answer was buried in Danielβs link (but not mentioned as the best), I decided that I would simply post the answer, so no one was tempted to return an empty answer which, as Michael points out, is not perfect .
The solution is to use the standard view and return an HttpResponse with raw data making up a single pixel gif. You cannot get to disk or redirect this huge advantage.
Please note that the url template uses the tracking code as the image name, so there is no obvious code = jf8992jf in the url.
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'), )
And here is a look. Note that it uses cache_control to prevent crazy requests. Firefox (along with many email clients), for example, will request an image twice each time for some reason, which probably does not bother, but need to worry about. By adding max_age = 60, you will receive only one request per minute.
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 """
dotcomly
source share