Last active
December 23, 2015 04:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head></head> | |
<body> | |
<img src="//chart.googleapis.com/chart?cht=qr&chs=177x177&ch1={{ url|urlencode }}"> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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