applegrew / django-select2

This is a Django integration for Select2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

form.media.css / form.media.js do not return anything in templates

tunecrew opened this issue · comments

Environment is django 2.2.3 / python 3.7.4.

django_select2 is installed and added to INSTALLED_APPS

{{ form.media.css }} and {{ form.media.js }} are added to the head and body sections of the template per the instructions.

Observed Behaviour:

No select2 elements are rendered. Inspection of the page source reveals that these template tags do not resolve to anything- there's just blank space where they were inserted.

Expected Behaviour:

select2 elements rendered, template tags replaced with appropriate links to css and js.

Workaround:

Replace template tags with explicit calls to css and js files, e.g.:

<link rel="stylesheet" 
  href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.css"
  integrity="sha256-cxYyFTq8AlfZjXRMeAy8KPHpmNVSpUNhnxQwNfUT0Lo="
  crossorigin="anonymous" />
<script
  src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/js/select2.min.js"
  integrity="sha256-GHC3xFbrevQ0aRcWg5JElUOncXYXxTtMOuA74cWAPTw="
  crossorigin="anonymous">
</script>

<script
  src="{% static 'django_select2/django_select2.js' %}">
</script>

note the necessity to refer to both select2.js and django-select2.js

Related Issue:

The documentation makes no reference to having to run collectstatic in order to copy the django_select2.js file to staticfiles.

Hi @tunecrew thanks for reaching out. Let's see, I am sure we can find solution together.
My first guess, is, that you don't an object named form in your template's context.

Can you provide me with the code of your view, so I can further help to debug the problem?

Thanks!
Joe

Hi - you're absolutely correct - the form in the view in question has a different name - let me update and see if that resolves. Perhaps this is worthy of a documentation mention in setup?

Confirmed this is the issue and it is works as expected if the form is called form in the context.

A couple thoughts re. this:

  • the docs should mention this explicitly - it seems obvious once you figure out the problem, but it is not obvious per the instructions.

  • so my template setup (like many persons I suspect) has a common header and footer that is included by other pages, meaning that the {{ form... }} tags belong in these common header/footer templates. This then forces the use of a form named form on any page using django-select2, which may require refactoring or may not fit well into a page with formsets or some kind of dynamically generated forms. I don't have a solution offhand, but will think about it. But back to the docs - mentioning what is actually included (the css and js files) would be valuable as well, so persons can take this route.

@tunecrew you make some good points. We're always interested to improve our documentation. Since you just went through the trouble yourself to set up this package, it would be amazing if you could provide a pull-request improving our documentation.

This repo has moved, please address your issue at https://github.com/codingjoe/django-select2 Thanks!