Wagtail: Read & parse the contents of an uploaded file

written by Jeff on 2024-09-21

In Wagtail I wanted a clean way to upload a file (like CSV or JSON) to the admin, parse it, and make it available to the front-end. This is handy for small chunks of data that you don't want to have to build a model for, or edit/update field-by-field each time the data changes. Instead, editors can upload their data file to the admin as a simple bulk data import. For my purposes, this was used for map coordinates displaying multiple pins and editors already have an auto-generated CSV file. So the aim was to prevent them from having to manually input new data each time it was updated.

Here's what I came up with:

In your model:

Now in your template you can reference {{ page.get_file_contents }} and use it in a for loop, etc.