Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django_select2.js is not loaded correctly when using a CDN in production #221

Closed
mlorant opened this issue Nov 3, 2015 · 8 comments
Closed
Assignees

Comments

@mlorant
Copy link

mlorant commented Nov 3, 2015

Hi,

I am using Amazon S3 to serve my static files in production. Since the upgrade to version 5, the file django_select2.js is not served correctly. Indeed, Django tries to fetch mywebsite.com/static/django_select2/django_select2.js instead of the Amazon S3 file.

I hotfixed this by editing Select2Mixin:

# Add the import
from django.contrib.staticfiles.templatetags.staticfiles import static

# Note the static call on the second JS file
return forms.Media(
     js=('//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js',
          static('django_select2/django_select2.js')),
     css={'screen': ('//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css',)}
)

It works fine in local and with local static serving though. I did not try my patch on other environments, so I don't know if this have consequences on other uses.

@codingjoe
Copy link
Collaborator

hm.... that's because of the static thing. That is actually wrong. We should fix it.

@codingjoe codingjoe added the bug label Nov 3, 2015
@codingjoe codingjoe self-assigned this Nov 3, 2015
@codingjoe
Copy link
Collaborator

Sorry. I misread you ticket.
The static widget media is defined as it should be. I must presume that the error is in your configuration. Please double check with https://docs.djangoproject.com/en/1.8/topics/forms/media/#paths-in-asset-definitions

@codingjoe codingjoe added not a bug and removed bug labels Nov 3, 2015
@mlorant
Copy link
Author

mlorant commented Nov 3, 2015

Mmmh, ok. Seems weird though, since it can needs more than "be prepended with the value of the appropriate prefix".

In my case, the static URL is https://<myAppID>.cloudfront.net/django_select2/django_select2.<hash>.js (where myAppID and hash are placeholder for unique string here of course.), hence the need of the staticfunction call. Django media forms does not seem to support that but I guess my problem is more framework-related and does not concerned this lib in particular :)

@codingjoe
Copy link
Collaborator

Seems so. Curious tho, please approach Tim code.djangoproject.org. I'd really like to clarify this issue.
Please post the ticket number here to allow other to read up on this issue.

@mlorant
Copy link
Author

mlorant commented Nov 4, 2015

I found a ticket specifying the same problem, created two years ago (but updated recently): https://code.djangoproject.com/ticket/21221 Last comment's author uses the same trick to bypass the problem...

@codingjoe
Copy link
Collaborator

@mlorant I commented on the issue. Let's see where it goes. Should there be an action form my side required, I will do so during django under the hood.

mlorant added a commit to mlorant/django-select2 that referenced this issue Nov 10, 2015
mlorant added a commit to mlorant/django-select2 that referenced this issue Nov 10, 2015
mlorant added a commit to mlorant/django-select2 that referenced this issue Nov 10, 2015
@codingjoe
Copy link
Collaborator

@mlorant I wrote a patch for django django/django#5571 (comment)
You'll have to wait until version 1.10 tho. I'd advice you overwrite media in your own code, including a note to remove you hack when upgrading to 1.10.

Cheers, Joe

@mlorant
Copy link
Author

mlorant commented Nov 12, 2015

@codingjoe I subscribed to the PR, so I followed activity about it. I also did a fork of the plugin, which includes the static call (see commit reference above). I will try to keep it up to date with your project, so everyone can use it if they have the same problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants