0

I'm trying to update an HTML canvas element on form submission with Django.

I have my views.py that renders the index.html page with the processed data in JSON.

return render(request, 'index.html', {'form': form, 'data': mark_safe(json.dumps(data))})

This render method is inside of an if request.method == 'POST', so essentially what's happening is it's sending the data through context to the HTML and JavaScript appropriately, however, I need canvas updates when I use the "submit" button.

$('#form').submit(function() { /* all of my canvas logic */ }); Momentarily, the canvas will show the sign before the Django page is refreshed, and then reset to a blank canvas.

Presumably this is because nothing in views.py explicitly tells the index.html to have a new canvas on the page with data, but I can't seem to find any information or docs on this. Furthermore:

  • Python tkinter or other GUI libraries don't really apply here.
  • I can only change and update the canvas through JavaScript.
  • I can't pass a whole HTML canvas element as context through the view.
  • e.preventDefault(); would cancel the whole submission; so if an input in the form changes, the POSTed data will not update, and in result, the drawn shapes on the canvas will not change.

Maybe there's a way to draw this after the form data is processed?

TLDR: How do I render a canvas based off of form data, and then have the canvas persist (or update) on the page after form submission?

erichards
  • 1
  • 3

1 Answers1

0

So, I ended up figuring out that I thought of how this data is being submitted completely wrong, and for my project specifically, there was a different solution that utilizes Django much better: actually using context. Oops!

But, for future reference on this issue in specific, there still may be solutions:

erichards
  • 1
  • 3