Skip to content

Instantly share code, notes, and snippets.

@myles
Last active December 23, 2015 04:49
Show Gist options
  • Save myles/6582787 to your computer and use it in GitHub Desktop.
Save myles/6582787 to your computer and use it in GitHub Desktop.
QR Code for Django - Appends a /qr/ to all your urls with a QR code generated by Google Charts.
<!DOCTYPE html>
<html>
<head></head>
<body>
<img src="//chart.googleapis.com/chart?cht=qr&amp;chs=177x177&amp;ch1={{ url|urlencode }}">
</body>
</html>
from django.conf.urls import patterns, url
from .views import QRCodeView
urlpatterns = patterns('',
url(
regex = r'^(?P<path>.*?)qr/$',
view = QRCodeView.as_view(),
name = 'qrcode',
)
)
from urlparse import urljoin
from django.views.generic.base import (
View,
ContextMixin,
TemplateResponseMixin
)
from django.contrib.sites.models import Site
class QRCodeView(TemplateResponseMixin, ContextMixin, View):
template_name = "qrcode.html"
def get(self, request, path, *args, **kwargs):
site = Site.objects.get_current()
url = urljoin("http://" + site.domain, path)
context = self.get_context_data(url=url)
return self.render_to_response(context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment