I have a basic model:
class MyModel(models.Model): my_field = models.CharField()
I have a basic form for this model:
class MyFrom(forms.ModelForm): class Meta: model = MyModel
And I have a function that performs a basic search (much more complicated in practice, there will be no regular expression, etc.):
POSSIBLE_VALUES = ['aa', 'bb', 'cc', 'dd'] def lookup(some_value): if some_value in POSSIBLE_VALUES: # the value is OK, return a string return some_value else: # constructs the 'did you mean' list of suggestions didyoumean = [pv for pv in POSSIBLE_VALUES if pv in some_value] # returns a list which might be empty return didyoumean
Now, the script I want:
- On the site, enter a value in the "my_field" input field and click the "Submit" button.
- If the value passes the check, I should automatically execute the form action.
- If I get several possible values, I have to display them to the user and not perform any other actions.
- If I have no answers (empty list), I should receive an error message.
Some additional requirements:
- I would rather have the โyou meanโ list displayed without reloading the page.
- If the user clicks on one of the sentences, I want to execute the form action without additional search - this value has already been verified.
- I want to keep all the logic outside the view and save it in the form or in the model. It's necessary.
- I want to avoid hardcoded js in the template and, if possible, paste it into the form. It's not obligatory.
Therefore, I assume that all of this will be distributed between this field check and custom widgets that would handle "you mean" list rendering. I just can't put it all together.
Your help is not required :)
EDIT. Ad. 2 in the requirements. This is the main feature that I described. In a more advanced mode, I want to have more fields in this form, so the list of โyou had in mindโ should be displayed along with all other field errors (if any). Then clicking on the tooltip will simply set my_field to it without reloading the form. The user will have to correct other errors as well, so I cannot immediately take the form. Maybe there is only some flag for switching between these two parameters ("basic" and "advanced").
source share