How to Build a QR Code Generator in Django (Step-by-Step Guide)
Source: Dev.to
Introduction
QR codes are widely used for payments, marketing campaigns, Wi‑Fi sharing, and WhatsApp links. In this tutorial we’ll build a simple QR‑code generator using Django and Python.
Installation
pip install qrcode[pil]
View
Create a view in your Django app:
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")
You can now access the generator at:
http://localhost:8000/generate_qr/?data=Hello
The QR code will be generated dynamically.
URL Configuration
Add the view to urls.py:
from django.urls import path
from .views import generate_qr
urlpatterns = [
path("generate_qr/", generate_qr),
]
Extending the Project
You can support additional QR‑code types, such as:
- Wi‑Fi QR codes
- WhatsApp QR codes
- Email QR codes
- vCard contact QR codes
- Custom colors
- Logos inside QR codes
Example: Generating a WhatsApp QR code
phone_number = "1234567890"
data = f"https://wa.me/{phone_number}"
Deployment Considerations
- Add caching for performance
- Implement rate limiting
- Optimize SEO for each QR type
- Add multilingual support
- Create a frontend interface
Production Example
A production‑ready example with multilingual support and multiple QR formats is available at:
fastools.online/en
Use Cases
- SaaS tool
- Marketing utility
- Developer tool
- Multilingual QR platform
Conclusion
Building a QR‑code generator in Django is simple and scalable. Django’s flexibility makes it easy to turn this project into a powerful tool for various applications.
Happy coding!