bradleyg / django-s3direct

Directly upload files to S3 compatible services with Django.
MIT License
653 stars 234 forks source link

File input doesn't show up #244

Closed WollenMoth closed 2 years ago

WollenMoth commented 2 years ago

I'm trying to use django-s3direct on the admin page. Followed the steps to use the example, but inputs doesn't appear in admin form.

image

I'm using Python 3.10 and Django 4.0.2

I would like to use django-s3direct in other proyect with the same versions, so I cannot downgrade.

WollenMoth commented 2 years ago

Form page doesn't work either.

image

And I'm getting this error on the browser console.

image

jwqian99 commented 2 years ago

I encountered the same error using the latest (2.0.0) release, downgraded to 1.1.8 worked.

python 3.10, djanog 3.25, djang-s3direct 1.1.8

byteofwood commented 2 years ago

I think I discovered what is causing this issue. s3direct's script tag is inserted in the head. This means that this part of s3direct's script (the last ~150 chars of /static/s3direct/dist/index.js, prettified) is run before the page is fully loaded.

...
new MutationObserver((function(t) {
    [].forEach.call(document.querySelectorAll('.s3direct'), A)
}))
    .observe(document.body, {
        childList: !0,
        subtree: !0
    })

I believe document.body is null at the time it is run which causes the "parameter 1 is not of type 'Node'." I bodged this by putting the script at the end of the body by adding to the admin template. Info in this SO answer.

My /templates/admin/base_site.html file:

{% extends 'admin/base_site.html' %}
{% load static %}

{% block footer %}
    <div id="footer"></div>
    <script src="/static/s3direct/dist/index.js"></script>
{% endblock %}

This fixes the input not showing up problem for me. Hopefully there is a better way to solve this, perhaps jquery's .ready(), javascript's window.onload, or by somehow telling django to put the script at the end of the body (I'm not sure if this is possible using the Media class), or maybe even putting the script in the widget's template.

bradleyg commented 2 years ago

Should be fixed in the latest release.