How to Build a QR Code Generator in Django (Step-by-Step Guide)
QR Codes are widely used for payments, marketing campaigns, WiFi sharing, and WhatsApp links.
In this tutorial, we’ll build a simple QR Code generator using Django and Python.
Step 1 — Install the Required Library
Install the qrcode package:
pip install qrcode[pil]
Step 2 — Create the View
Inside your Django app, create a view:
import qrcode
from django.http import HttpResponse
from io import BytesIO
def generate_qr(request):
data = request.GET.get("data", "Hello World")
qr = qrcode.make(data)
buffer = BytesIO()
qr.save(buffer, format="PNG")
return HttpResponse(buffer.getvalue(), content_type="image/png")
Now you can access:
http://localhost:8000/generate_qr/?data=Hello
And the QR code will be generated dynamically.
Step 3 — Configure URLs
In urls.py:
from django.urls import path
from .views import generate_qr
urlpatterns = [
path("generate_qr/", generate_qr),
]
Step 4 — Extending the Generator
You can extend this project to support:
- WiFi QR Codes
- WhatsApp QR Codes
- Email QR Codes
- vCard contact QR Codes
- Custom colors
- Logo inside QR
For example, generating a WhatsApp QR:
data = f"https://wa.me/{phone_number}"
Production Considerations
If you're planning to deploy this:
- Add caching for performance
- Implement rate limiting
- Optimize SEO for each QR type
- Add multilingual support
- Create a frontend interface
If you'd like to see a production-ready example with multilingual support and multiple QR formats, you can check out:
Final Thoughts
Building a QR Code generator in Django is simple and scalable.
You can turn this into:
- A SaaS tool
- A marketing utility
- A developer tool
- A multilingual QR platform
Django makes it flexible and powerful.
Happy coding!
Top comments (0)