I have been using this view for searching a word
as:
db
refers mongo connection (just for ref)
@app.route('/')
def index():return render_template('index.html')@app.route('/words-<word>', methods=['GET', 'POST'])
def wordsearch(word):collection_name=word[0].lower()+'_collection'words=db[collection_name]data=words.find({'word':word})return render_template('wordsearch.html',data=data)
In index.html
template I have been doing this to match this above url as:
<script type="text/javascript">$(document).ready(function(){$('#submit').on('click', function() {var wordvalue = $("#word").val(); //getting word from element ID window.location.href ="/"+"words-"+wordvalue; //match the URL in view})});</script>
Does this can be done in more dynamic way ?, I mean this only works for word
and not for other selections,or combination of selections as below:
The search input looks as:
word length: ()
word type : ()
word : ()submit
Now the API I have does match only if I send word , but how can I write a single API such that it should match all the possible combinations like word length + word type
, word + word type
(queries I would define on own)
What I have tried is :
@app.route('/<n>-letter-words', methods=['GET', 'POST'])
@app.route('/words-<word>', methods=['GET', 'POST'])
def wordsearch(word=None,n=None):if word:collection_name=word[0].lower()+'_collection'words=db[collection_name]data=words.find({'word':word})return render_template('wordsearch.html',data=data)data = 'you are searching with' + n + 'words'return render_template('lettersearch.html', data=data)
and in templates the script
as:
<script type="text/javascript">$(document).ready(function(){$('#submit').on('click', function() {var lettervalue = $("#wordlength").val();var wordvalue = $("#word").val();if (lettervalue==''){window.location.href ="/"+"words-"+wordvalue;}else{window.location.href ="/"+lettervalue+"-letter-words";}})});</script>
But confused if there are combination's like, 6-letter-words-of-verbs
verb is a word type
here
Also how to match the same URL for these combination's from template as i was doing with JQuery used in script
above?
Is this the correct way? , I guess writing all possible routes in views and match it from template with the conditions in Jquery is a bad idea ,
any help/guiding links are appreciated ,TIA