I try to implement search mechanism in Django but I couldn’t figure out what is the best way to do it.
I have a combobox and a textbox as seacrh parameters. There are two options in combobox like “A” and “B” and depending on that option different views should be run.
However, because there is a common properties of these two options, such as min-max length of textbox and soon on, I consider when search button clicked, it should go to some common search view and after the textbox is validated,or not, depending on the option in the combobox proper page should be redirected to proper page with the request given.
main/views.py
def search(request):
if request.method == 'POST':
type = request.POST['searchType']
searchvalue = request.POST['searchvalue']
if len(searchvalue) > 200:
return render_response(request, 'search/error.html', {'message':'bidi'})
if type == 'A':
return redirect('/A/search/')
elif type == 'B':
return redirect('/B/search/')
else:
return render_response(request, 'search/error.html', {'message':'errormessa'})
A/views.py
def search(request):
...
B/views.py
def search(request):
...
However, as you see, while I redirect to view A or view B I need to pass searchvalue as a post paramater but I couldn’t do.
Besides, I wonder is my structure proper and secure way to do such search ? What is the best approach to implement such search ?
Thanks
If you know javascript, do the search(form submit?) from javascript; so basically you check what the user has chosen and suitably chose the endpoint.