JSONDecodeError when iterating Jsignature in template
Example :
In view:
context = {
'bills' : bill.objects.all()
}
models:
class Bill(models.Model):
signature_name = models.CharField(max_length=255, blank=True)
signature = JSignatureField(blank =True)
template :
{{ bills.media }}
{% for bill in bills %}
<p>{{bill.billNumber}}</p>
<img src="{{ bill.signature | signature_base64 }}" alt="">
{% endfor %}
Django Debug output
<html>
<body>
<!--StartFragment--><h1 style="padding: 0px; margin: 0px; font-weight: normal; color: rgb(0, 0, 0); font-family: sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">JSONDecodeError at /test/</h1><pre class="exception_value" style="padding: 0px; margin: 10px 0px; font-size: 1.5rem; white-space: pre-wrap; word-break: break-word; font-family: sans-serif; color: rgb(87, 87, 87); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Extra data: line 1 column 961 (char 960)</pre>
Request Method: | GET
-- | --
http://127.0.0.1:8000/test/
5.1.5
JSONDecodeError
Extra data: line 1 column 961 (char 960)
C:\Users\snc_p\AppData\Local\Programs\Python\Python313\Lib\json\decoder.py, line 348, in decode
workingBill.views.testView
C:\Projet\git\BonDeTravail\.venv\Scripts\python.exe
3.13.1
['C:\\Projet\\git\\BonDeTravail', 'C:\\Users\\snc_p\\AppData\\Local\\Programs\\Python\\Python313\\python313.zip', 'C:\\Users\\snc_p\\AppData\\Local\\Programs\\Python\\Python313\\DLLs', 'C:\\Users\\snc_p\\AppData\\Local\\Programs\\Python\\Python313\\Lib', 'C:\\Users\\snc_p\\AppData\\Local\\Programs\\Python\\Python313', 'C:\\Projet\\git\\BonDeTravail\\.venv', 'C:\\Projet\\git\\BonDeTravail\\.venv\\Lib\\site-packages']
Fri, 31 Jan 2025 14:56:55 -0500
<!--EndFragment-->
</body>
</html>
Hi,
I made minor change in jsignature_filters.py for fixing the issues.
@register.filter
def signature_base64(value):
if(str(value).endswith('v')):
value = value[:-1]
if value is None or not isinstance(value, str):
return ""
in_mem_file = io.BytesIO()
draw_signature(value).save(in_mem_file, format="PNG")
in_mem_file.seek(0)
return "data:image/png;base64,{}".format(
iri_to_uri(base64.b64encode(in_mem_file.read()).decode('utf8'))
)
I dont understand exactly from where the 'v' is added but now it's working.
I don't recommend modifying the source code, it may be that the v is added by your changes. If the v comes from the database, that may be your input form which brings the issue.
I checked the database requested data, no V. It's only appear when having multiple signature_base64 filter tag in template.
I'll make further research later Where the V come from but the database request data contain no ending v.
Téléchargez Outlook pour iOShttps://aka.ms/o0ukef
De : Sébastien Corbin @.> Envoyé : Monday, February 3, 2025 6:33:48 AM À : fle/django-jsignature @.> Cc : knarfel @.>; Author @.> Objet : Re: [fle/django-jsignature] JSONDecodeError when iterating Jsignature in template (Issue #44)
I don't recommend modifying the source code, it may be that the v is added by your changes. If the v comes from the database, that may be your input form which brings the issue.
— Reply to this email directly, view it on GitHubhttps://github.com/fle/django-jsignature/issues/44#issuecomment-2630692148, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AY6MBM3BYQMGQXEPLRHK2L32N5HZZAVCNFSM6AAAAABWIKEERGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMMZQGY4TEMJUHA. You are receiving this because you authored the thread.Message ID: @.***>