Django - a custom widget with "you mean" prompts based on verification

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").

+4
source share
1 answer
  • I would prefer the "you mean" list to appear without the need to reload the page.

Create your own widget that displays using JS code to check for possible values โ€‹โ€‹when the user enters it

  1. If the user clicks on one of the sentences that I want to perform a form action without an additional lookup, the value has already been checked.

Again, this widget, by clicking on it, should just submit the form.

  1. I want to save all the logic outside the view and save it in forms or models. It's necessary.

In the form, you will have a clean () method to check everything. If, say, some dummy data is sent with p. 2 - you are still causing a validation error.

  1. I want to avoid hardcoded js in the template and paste it into the if form possible. It's not obligatory.

Solved using a custom widget in more detail .

+2
source

Source: https://habr.com/ru/post/1299546/


All Articles