1

my use case is:

I generate in some view with certain url qrcode, and then I use it in template like this:

<img src="/qrcode/2">

I know holder.js and I would like to display something like:

<img src="holder.js/200x200/text:qrcode not avaliable">

when the qrcode image is not loaded becasue of anything (like bad id, soem crash of PIL library...).

What will be the most clean solution?

I was trying in view:

return HttpResponse("holder.js/200x200/text:qrcode not avaliable", content_type="image/png")

also with mime type set to text, but it is not replacing the source src in img tag.

UPDATE:

I think for a while, and I started being afraid, that I am trying to reinvent the wheel... So I decided to pass appropriate url in view rendering the page:

def certificate(request, pk):

    try:
        spot = Spot.objects.get(pk=pk)
        if request.method == 'GET':

            link = '/qrcode/%d' % int(pk)
            return render(
                request,
                'certificate.html',
                {'spot': spot, 'qrcode_link': link})

    except:
        return render(
            request,
            'certificate.html',
            {'spot': None,
             'qrcode_link': 'holder.js/200x200/text:qrcode not avaliable'})

And in template I just: <img src="{{qrcode_link}}">

But the question is still open and all ideas, how to do it better will be apriciated:-)

andilabs
  • 22,159
  • 14
  • 114
  • 151

1 Answers1

0

I think your second approach makes the most sense, however you don't have to include the Holder URL in your code. Holder 2.4 includes a 404 fallback, so that any images that fail to load will show up as a placeholder. You can use it like this:

<img data-src="holder.js/200x200/text:QR Code Unavailable" src="/qrcode/123">

If the image at /qrcode/123 is not found or can't load, the placeholder will be displayed instead.

imsky
  • 3,239
  • 17
  • 16