I made an app to generate barcodes using python-barcode library (Python)(django framework)
and there's two fields
1 - to let the user input the number that will used for generate the barcode
2 - options to let the user select which type of barcode he wants
for now everything work fine it's generating the barcodes
but some of the barcodes have its specific types of inputs like 12 digits or 8 digits and so on
so how to check the inputs if its valid for that type of barcode or not?, then show error messages if it's wrong, or succes message if it generated fine
I know I can use messages form django itself but how I can implement it with checking function?
for views.py
from sre_constants import SUCCESS
from django.http import HttpResponse
from django.shortcuts import redirect, render
import barcode
from barcode.writer import ImageWriter
import tempfile
from django.contrib import messages
def index(request):
context = {'barcode_types':barcode.PROVIDED_BARCODES}
return render(request, 'index.html',context)
def generate_barcode(request):
inputs = request.POST['inputs']
types = request.POST['types']
barcode_class = barcode.get_barcode_class(types)
file = tempfile.NamedTemporaryFile()
code = barcode_class(inputs,writer=ImageWriter())
file_path = code.save(file.name)
response = HttpResponse(open(file_path,'rb').read(), headers={'Content-Type': 'image/png','Content-Disposition': 'attachment; filename="'+inputs+'.png"'})
return response