jmrivas86 / django-json-widget

An alternative widget that makes it easy to edit the new Django's field JSONField (PostgreSQL specific model fields)
MIT License
433 stars 88 forks source link

Use relative path for media assets #23

Closed toracle closed 4 years ago

toracle commented 5 years ago

I had a problem with asset URL, and inspected into it.

css and js property of django Media class is rendered by render_js and render_css method.

When look into that method, it calls absolute_path method to make a path absolute, and absolute_path calls static function, and it calls StaticNode.handle_simple method.

And, this handle_simple method do the same things you do, turn a path to absolute one with storage.url().

So, default django Media logic act like this:

  1. If js or css path is an absolute path, use itself.
  2. If they are relative one,
    1. If django.contrib.staticfiles app is installed, get a staticfile_storage which is defined by settings.STATICFILES_STORAGE value, and use storage.url(path) to convert it to absolute.
    2. If the app is not installed, just add STATIC_URL prefix to the path.

So, I think we can simply to remove any URL prefix and using a relative path would works well.

frost-nzcr4 commented 5 years ago

Previous versions of Django (1.9-1.11) use the same logic, so STATIC_URL should be ommited.

submarcos commented 4 years ago

I confirm STATIC_URL should be removed. It doesn't work with external file storage backend (example: s3). I need to patch with

from django_json_widget.widgets import JSONEditorWidget

class JSONEditorWidget(JSONEditorWidget):
    """
    Override media infos ith compliant django style
    Wait for https://github.com/jmrivas86/django-json-widget/pull/23 merge
    """
    class Media(JSONEditorWidget.Media):
        css = {'all': ('dist/jsoneditor.min.css',)}
        js = ('dist/jsoneditor.min.js',)
jmrivas86 commented 4 years ago

I close this PR because is solved in #30