Code Monkey home page Code Monkey logo

Comments (5)

asyncee avatar asyncee commented on June 4, 2024

Hello!

First of all, let's check if form media was rendered properly. You should see select2 javascript libary in the page source code. More about media in the docs.

Secondly, try to pass instance of Select2Multiple instead of class.

Also you should check if there any javascript errors in browser console, which may prevent widget loading.

from django-easy-select2.

randomshinichi avatar randomshinichi commented on June 4, 2024

I tried passing an instance but it didn't make a difference.

There is no reference to the select2 javascript library in the page source code. No js file is being loaded from Django. Is this intended behaviour? Am I supposed to setup Form Assets myself? I thought including the Select2Multiple() widget would cause the required JS files to be loaded, since this was not mentioned in the documentation.

from django-easy-select2.

asyncee avatar asyncee commented on June 4, 2024

No. Django can handle form assets by itself:

Regardless of whether you define a media declaration, all Form objects have a media property. The default value for this property is the result of adding the media definitions for all widgets that are part of the form

That means dependent static files will be loaded automatically.

So it looks like you are missing {{ form.media }} in the page source. Try to add it to the <head> section of your page.

from django-easy-select2.

randomshinichi avatar randomshinichi commented on June 4, 2024

Oh yes, it works now!

Good practice question: is it good to add {{ form.media }} to base.html (because that contains the <head> section)? Then it will be run for every page, even the ones that don't have a form or use the Select2 widget.

Right now my base.html has this snippet in <head>:

    {% if form.media %}
      {{ form.media }}
    {% endif %}

from django-easy-select2.

asyncee avatar asyncee commented on June 4, 2024

As a good practice advice you can use this:

  1. in base.html you create a block, for example {% block extrahead %}{% endblock %}.
  2. override this block on every single page, where you need custom js, css, form or widget media, etc, like this:
{% block extrahead %}
    {{ form.media }}
{% endblock %}

This way you can work with pages, that have multiple forms, following DRY principle.

I think we can close it now.

from django-easy-select2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.