applegrew / django-select2

This is a Django integration for Select2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

misleading http status 404 on bad request

mfrasca opened this issue · comments

Describe the bug
I'm still learning how to get this to work, and I've been confused at a 404 error. my still incomplete code produces a GET /select2/fields/auto.json?term=4444&_type=query&q=4444 query, to which the server answers with a 404 status code.

on the console where I'm running the server (.manage.py runserver), I see this:

Not Found: /select2/fields/auto.json
[01/May/2019 15:00:09] "GET /select2/fields/auto.json?term=4444&_type=query&q=4444 HTTP/1.1" 404 1812

however, if I file the same request from a web browser, I see the real trouble: No "field_id" provided.

apart from the fact that I have no clue on how to provide the field_id, and that's a different issue, I'm concerned with the misleading 404 code. a more generic 400, or 406 would be clearer. even 418 would be less confusing.

further info
I am not sure of what is going on here, but I definitely have not been able to get the {{ form.media.css }} expanded, so I fell back to using the css includes I have copied from the admin. also the {{ form.media.js }} translates to the empty string. if I remove the admin css/js, I get django.urls.exceptions.NoReverseMatch: Reverse for 'auto-json' not found. 'auto-json' is not a valid view function or pattern name.

code snippets

this is not working:

class VerificationForm(ModelForm):
    accession = forms.ModelChoiceField(
        queryset=Accession.objects,
        widget=ModelSelect2Widget(
            data_view='auto-json',
            model=Accession,
            search_fields=['code'],
        )
    )

this is working, but uses the admin interface!

    contact = forms.ModelChoiceField(
        queryset=Contact.objects,
        widget=ModelSelect2Widget(
            data_url='/admin/collection/contact/autocomplete/',
            model=Contact,
            search_fields=['name'],
        )
    )

I found the reason for the wrong query, that is why field_id was not included in the query. I am producing an interface where the forms are passed via json to the javascript client page, and I am putting them into a Bootstrap modal, initializing them late. it was not clear to me that the initialization done by .djangoSelect2 is alternative to .select2, I can't use this function for adding options to the select2 elements.
still quite sure that 404 is not the right thing to say, when the script is there, it gets executed, but it gets the wrong parameters.

Hi @mfrasca
I agree the response code would be debatable, though the argument could go both ways. However, I think it would be better to actually provide status messages in DEBUG mode as well as better debug logs.

Anyways, you can find the documentation on how to initialize the select2 fields here:
https://django-select2.readthedocs.io/en/latest/django_select2.html#javascript

Best
-Joe

may I suggest you keep this one issue open, at least as long as the debug logs aren't clearer?
as said: the client receives a 404, the console shows Not Found: /select2/fields/auto.json, while the trouble is an incorrect request to an existing url, and you only get to see this if you open the url with the browser. and such a url is not meant to be opened from the browser, really. a 406 to the client would hint the programmer at checking the parameters. this 404 hints at checking the url configuration.

about 'how to initialize', I did not grab the slightly too implicit statement: the role of .djangoSelect2 is to replace the .select2 call. I had assumed its role was to complement the initialization. would it be possible to make the call non destructive? or I'll possibly include this replace role in my documentation PR.

@mfrasca the JavaScript part was written by better JS coders than me. I believe they implemented the best possible solution. That being said, I agree, it should be stated clearly in the documentation. Please include it in your pull-request.