jazzband / django-smart-selects

chained and grouped selects for django forms
https://django-smart-selects.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.12k stars 352 forks source link

bindfields.js:14 Uncaught ReferenceError: chainedfk is not defined - Django Admin #253

Closed chris-jones closed 4 years ago

chris-jones commented 6 years ago

Checklist

Put an x in the bracket when you have completed each task, like this: [x]

Error received within Google Chrome when loading Django admin page of a model with an inline model which uses smart-selects.

bindfields.js:14 Uncaught ReferenceError: chainedfk is not defined
    at initItem (bindfields.js:14)
    at HTMLSelectElement.<anonymous> (bindfields.js:35)
    at Function.each (jquery-2.2.0.js:360)
    at HTMLDocument.<anonymous> (bindfields.js:34)
    at fire (jquery-2.2.0.js:3182)
    at Object.fireWith [as resolveWith] (jquery-2.2.0.js:3312)
    at Function.ready (jquery-2.2.0.js:3531)
    at HTMLDocument.completed (jquery-2.2.0.js:3547)

Steps to reproduce

Environment:

  1. Configured Django Instance as specified in the installation and configuration guides.
  2. Smart Selects used on a Model, which is present as an InlineForm on another model Admin page
  3. Load page

Actual behavior

Nothing. Error in Javascript prevents the code being actioned.

Expected behavior

Smart-selects should load two fields normally, and provide smart select functionality between them.

I actually removed all other Javascript references and inline javascript code from the HTML in order to check that there wasn't a conflicting code issue, but found nothing.

The script files are loaded in the correct order, none are 404-ing:

<script type="text/javascript" src="/admin/jsi18n/"></script>
 <script type="text/javascript" src="/static/admin/js/core.js"></script>
 <script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
 <script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
 <script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
 <script type="text/javascript" src="/static/admin/js/actions.js"></script>
 <script type="text/javascript" src="/static/admin/js/urlify.js"></script>
 <script type="text/javascript" src="/static/admin/js/prepopulate.js"></script>
 <script type="text/javascript" src="/static/admin/js/vendor/xregexp/xregexp.js"></script>

 <script type="text/javascript" src="/static/smart-selects/admin/js/chainedfk.js"></script>
 <script type="text/javascript" src="/static/smart-selects/admin/js/bindfields.js"></script>

 <script type="text/javascript" src="/static/admin/js/calendar.js"></script>
 <script type="text/javascript" src="/static/admin/js/admin/DateTimeShortcuts.js"></script>

Can't for the life of me see what's wrong. I think the error being thrown-up might be misleading somehow? I even combined the smart-select Javascript together, so that there would be no scope issues.

nazrow commented 6 years ago

+1, experiencing the exact same thing. Neither chainedfk nor chainedm2m work where they should (I have chained fields of both types in like a dozen different models). Respective .js files are loaded from the same /static/smart-selects/admin/js folder where bindfields.js resides, yet console shows the functions are not defined.

I've tried cleaning up chainedm2m.js removing all the things I don't understand and leaving it just like this: function chainedm2m() { ... } Now the browser is able to find chainedm2m, but tells me that chainedm2m.init is not a function. Same goes for chainedfk.

nazrow commented 6 years ago

Aaaand it works now. I have no idea why chainedm2m.js and chainedfk.js are written the way they are, and I intend to continue to keep away from JavaScript for as long as possible, but for now I got it to work properly by editing chainedm2m.js this way:

var chainedm2m = {
    fireEvent: function (element, event) { ... },
    dismissRelatedLookupPopup: function(win, chosenId) { ... },
    fill_field: function (val, initial_value, elem_id, url, initial_parent, auto_choose) { ... },
    init: function (chainfield, url, id, value, auto_choose) { ... }
};

{ ... }s mean that I changed nothing within the actual methods themselves.

Same goes, again, for chainedfk.

GrilloLuca commented 6 years ago

Hi, just tried the same exact thing and it workd for me too but with var chainedfk =


var chainedfk = {

        fireEvent: function (element, event) { ... },
        dismissRelatedLookupPopup: function (win, chosenId) { ... },
        fill_field: function (val, init_value, elem_id, url, empty_label, auto_choose) { ... },
        init: function (chainfield, url, id, init_value, empty_label, auto_choose) { ... }
};
mfaulkner9294 commented 6 years ago

I'm also experiencing this issue...

christianalcantara commented 6 years ago

Solution: https://github.com/RafaDias/django-smart-selects/commit/61f182f4e56fa7f7eb1ca2fbf0fb922bb25c8a0e

ShubhamSarda commented 6 years ago

Thanks it actually help to remove Error "bindfields.js:14 Uncaught ReferenceError: chainedfk is not defined - Django Admin", but now have constant error TypeError: $ is not a function, since we removed it as function.

blag commented 6 years ago

Hi everybody, I think I fixed this in the js-unlinting-fixes branch. If you are still having the issue, can you try that and tell me if it works?

You should be able to install that branch using pip:

pip install git+https://github.com/digi604/django-smart-selects.git@js-unlinting-fixes
ashkulz commented 6 years ago

@blag: works for me on Django 1.11, thanks for the fix!

openrijal commented 6 years ago

is there a roadmap when this is about to be merged in master and that can be available to PyPi?

openrijal commented 6 years ago

@blag I installed it through the branch you mentioned, I see that the error is gone, but when I selected one of the chained field, the JSON response has valid data, but it never appears on the dropdown.

screen shot 2018-04-06 at 2 56 52 pm
openrijal commented 6 years ago

this fixes it https://github.com/digi604/django-smart-selects/issues/56#issuecomment-379360369

ShubhamSarda commented 6 years ago

Fixed Worked Well, Make Sure You Allow Django JQuery from settings.py.

blag commented 6 years ago

@crackjack Your fix should work for that case, but I'm confused where j.response is coming from. From all of the code I have seen, and from the jQuery documentation for $.getJSON, the j.errors and j.response aren't coming from django-smart-selects. 😕 Can you figure out where those two keys are coming from?

openrijal commented 6 years ago

$.getJSON calls a url, the URL depends on the model name and the app name which finally calls one of the methods in views.py that returns a JSON response with the error and the response objects. @blag

blag commented 6 years ago

@crackjack Right, that much I followed, but I'm confused where the JSON response is getting constructed that contains the errors and response keys. We need to track that down before deciding if this fix is correct or not.

openrijal commented 6 years ago

@blag from the HttpResponse method. https://github.com/digi604/django-smart-selects/pull/259#discussion_r182474797

developer2806 commented 6 years ago

Hi somehow not working for me.. I am using Python 3.6.4 Django 2.0.4 In Settings.py Tried

USE_DJANGO_JQUERY = True USE_DJANGO_JQUERY = False USE_DJANGO_JQUERY = None

Tried https://github.com/digi604/django-smart-selects.git@js-unlinting-fixes as well as Master Still having the same issue

bindfields.js:12 Uncaught ReferenceError: chainedfk is not defined at initItem (bindfields.js:12) at HTMLSelectElement. (bindfields.js:31) at Function.each (jquery.js:365) at HTMLDocument. (bindfields.js:30) at fire (jquery.js:3187) at Object.fireWith [as resolveWith] (jquery.js:3317) at Function.ready (jquery.js:3536) at HTMLDocument.completed (jquery.js:3552)

Can anybody suggest please.

chris-jones commented 6 years ago

Sorry it's been so long responding, I've was moved across to other projects.

Just tried the js-unlinting-fixes branch which works well for me. Anyone still having problems, just check that you run pip with the --upgrade flag, and also remember to run manage.py collectstatic (if you're using the static serving option).

ricardodani commented 6 years ago

@blag works for me on django 2.0. Thank you

https://github.com/digi604/django-smart-selects/issues/253#issuecomment-377007803

CJonesie commented 6 years ago

I had the same issue and this branch fixed it for me! https://github.com/digi604/django-smart-selects.git@js-unlinting-fixes

thanks!

basilrabi commented 6 years ago

js-unlinting-fixes branch fixes the issue for me too. Thank you!

developer2806 commented 6 years ago

Thanks worked for me too. Had to clear the cache and then it worked. But now facing the issue while edit.

Facing the issue while editing the chained m2m field. The selected options does not appear first while editing.

But if you change the main selection for the chained field then change it back to the original value the correct data loads in the corresponding multi selected field.

Any fixes for this please?

hjalves commented 6 years ago

Having the same problem as @developer2806

basilrabi commented 6 years ago

For me, the 2nd-order(or more) chained foreign key field does not show its previously set value. The 1st chained foreign key field, has no issue and displays its previously set value. Maybe I am having similar issues with @developer2806 and @hjalves . When I tried the patch in #260 , the issue I'm experiencing is fixed.

basilrabi commented 6 years ago

Just found out that js-unlinting-fixes does not work in AdminInline. I tried looking #136 but I don't know JS.

richard-reece commented 6 years ago

Just ran into this and https://github.com/digi604/django-smart-selects.git@js-unlinting-fixes solved the issue for me, thanks @blag !

ishakoktn commented 6 years ago

On version 1.5.4 have same problem as developer2806's. I turned 1.5.3 and it works.

chingmeng commented 6 years ago

Worked on Firefox, but not on Chrome?

ferminarellano commented 5 years ago

js-unlinting-fixes branch fixes the issue for me. Thank you! Make sure to use the --upgrade flag when running the pip instruction and then run the collectstatic script. Using Django 1.11

chingmeng commented 5 years ago

Worked on Firefox, but not on Chrome?

Okay, after getting the code from js-unlinting-fixes branch and clearing the browsing history, it works! Thank you!

Kras4ooo commented 5 years ago

Hey @digi604 Could you merge this branch: js-unlinting-fixes it works well on Django 2.1.7 too.

Thanks in advance!

Taher5253 commented 4 years ago

Uncaught ReferenceError: chainedm2m is not defined at initItem (bindfields.js:14) at HTMLSelectElement. (bindfields.js:25) at Function.each (jquery.min.js:2) at bindfields.js:24 at dispatch (jquery.min.js:3) at r.handle (jquery.min.js:3)

Getting this error on ChainedManyToManyField

How can i resolve this ?

kciredor commented 4 years ago

I'm giving up on this package. Anyone got a proper alternative?

Norserium commented 4 years ago

If you are out there, if anyone is out there, there is solution of this problem.

The problem appeared in 1.5.4. In this commit, to be exact.

I suppose, that @blag add "use strict"; line to correspond linter rules. Then he noticed, that it produces error ReferenceError: chainedfk is not defined and rewrote the code, to replace the variable definition on function definition. But he forgot, that function that defined in another function won't be global, so this change is the reason of this bug.

So, to solve this problem you can install 1.5.3 version or try to get this library not from PyPi, but from the repository directly (this problem is solved here).

Norserium commented 4 years ago

@manelclos you've closed this issue too early. Could you release a new version in PyPi? The most of users uses this package manager to install libraries for Python.

manelclos commented 4 years ago

@Norserium no problem in reopening if this is still a problem. Did you reproduce using master branch?

Norserium commented 4 years ago

@manelclos, you misunderstood me. The problem is in the last version of package in PyPi 1.5.4. I suppose it's needed to publish new version with the last updates, because users will still encounter this problem in the future.

manelclos commented 4 years ago

@Norserium I've got access to pypi now, I'll release ASAP

Norserium commented 4 years ago

@manelclos, just for your information I've just installed the package from the repository (master branch). This error is not reproducing now (and it shouldn't according to the last changes in the code).

manelclos commented 4 years ago

Closing as it works on latest release.