I am trying to call a function to print a receipt using Django in an html file. The function has 1 variable print_order_receipt(number) where the number is the order number fetched from Shopify API. This is the HTML code I did:
path('print/<int:number>/', views.print_order_receipt, name="print")
And this is the function:
def print_order_receipt(number):
order = Order.objects.get(number=number)
printer = Network("192.168.1.100")
printer.text('**********************************************\n')
printer.text('LNKO vous remercie!\n')
printer.text('**********************************************\n')
printer.text('\n')
printer.text('\n')
printer.text('Commande Numero: ')
printer.text(order.number)
printer.text('\n')
printer.text('\n')
printer.text('Montant total a payer: ')
printer.text(order.totalprice " MAD")
printer.text('\n')
printer.text('Dont taxes: ')
printer.text(order.tax " MAD")
printer.text('\n')
printer.text('Client: ')
printer.text(order.clientfname ' ' order.clientlname)
printer.text('\n')
printer.text('Une facture detaillee a ete envoyee a:\n')
printer.text(order.clientemail)
printer.barcode(order, 'EAN13', 64, 2, '', '')
printer.qr("You can readme from your smartphone")
printer.cut()
And these are the 2 HTML versions I tried, both are not working:
<td><a href="/print/{{ number }}">Imprimer</a></td>
<td><a href="{% url 'print' {{ number }} %}">Imprimer</a></td>
I don't know what I'm doing wrong here! Here is the Traceback error:
Environment:
Request Method: GET
Request URL: http://localhost:8000/print/12500/
Django Version: 2.2.5
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "D:\Dropbox\Dropbox\My Laptop\lnko\printer_receipt\venv\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "D:\Dropbox\Dropbox\My Laptop\lnko\printer_receipt\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "D:\Dropbox\Dropbox\My Laptop\lnko\printer_receipt\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /print/12500/
Exception Value: print_order_receipt() got multiple values for argument 'number'
CodePudding user response:
The first parameter of a function-based view is always the request, so:
def print_order_receipt(request, number):
# …
Django will make a call like printer_order_receipt(request, number=12500)
. If your first parameter is number
then both request
and number=12500
will try to set the value for the number
parameter hence the error.
If you use a django template tag, then you should not write variables between curly brackets, you write the URL as:
<td><a href="{% url 'print' number %}">Imprimer</a></td>
or:
<td><a href="{% url 'print' number=number %}">Imprimer</a></td>