1

This has probably been asked before, but after searching for awhile I'm still a bit confused. I'm trying to make a flask app but I'm not too familiar with JQuery itself, but I would like to create an autocomplete widget in my html. However, instead of querying a database I'd like to just use a static list and a regex to get results. I used this as reference.

What I did was:

@app.route('/autocomplete', methods=['GET'])
def autocomplete():
    search = request.args.get('q')
    #query = db_session.query(Movie.title).filter(Movie.title.like('%' + str(search) + '%'))
    #results = [mv[0] for mv in query.all()]
    results = ['Beer', 'Wine', 'Soda', 'Juice', 'Water']
    return jsonify(matching_results=results)

while keeping the rest of the code:

<head>
<link href="//code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="Stylesheet"></link>
<script src="//code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="//code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>

<script type="text/javascript">
$(function() {
    $("#autocomplete").autocomplete({
        source:function(request, response) {
            $.getJSON("{{url_for('autocomplete')}}",{
                q: request.term, // in flask, "q" will be the argument to look for using request.args
            }, function(data) {
                response(data.matching_results); // matching_results from jsonify
            });
        },
        minLength: 2,
        select: function(event, ui) {
            console.log(ui.item.value); // not in your question, but might help later
        }
    });
})

</script>
</head>


<body>
    <div>
        <input name="autocomplete" type="text" id="autocomplete" class="form-control input-lg"/>
    </div>
</body>

I haven't implemented a regex but I assumed if I typed anything with at least 2 characters I would get a dropdown of the list I wrote above. However, I get nothing. Any ideas on how I can get this autocomplete to work?

Community
  • 1
  • 1
L. Chu
  • 123
  • 3
  • 14
  • [Meaning ?](https://twitter.github.io/typeahead.js/) – dsgdfg Dec 17 '16 at 11:03
  • Your code seems to work for me. I commented-out the line that references `request.args.get('q')`, since that wasn't needed to just test the very basics. Can you tell us if you're getting any python errors or javascript errors? – YellowShark Dec 17 '16 at 20:40
  • @YellowShark No errors thrown, it's just that functionality is not there. On my webpage I get a text input box, but when I type an input to test it out the autocomplete doesn't happen. – L. Chu Dec 17 '16 at 20:50
  • @dsgdfg Yeah, that's basically what I'm looking for. I'll check it out if options are exhausted but I'm in a time crunch of 1 and a half days right now so hopefully what I have can be tuned slightly. Currently reading http://stackoverflow.com/questions/22449884/flask-jquery-autocomplete to see if I can get the autocomplete to work. Thanks for the share though! – L. Chu Dec 17 '16 at 20:54
  • And when you type in the input field, are you seeing any ajax requests in the inspector? I'm definitely seeing those requests happen once I've typed at least two characters into the field. – YellowShark Dec 17 '16 at 21:13
  • Scratch that, I keep getting this: `(index):32 Uncaught TypeError: $(...).autocomplete is not a function at HTMLDocument. (http://localhost:5000/:32:28) at i (http://code.jquery.com/jquery-2.2.0.min.js:2:27065) at Object.fireWith [as resolveWith] (http://code.jquery.com/jquery-2.2.0.min.js:2:27828) at Function.ready (http://code.jquery.com/jquery-2.2.0.min.js:2:29621) at HTMLDocument.J (http://code.jquery.com/jquery-2.2.0.min.js:2:29806)` – L. Chu Dec 18 '16 at 05:17
  • 1
    @yellowshark So it ended up being an issue with having jquery referenced twice (I re-refrenced after the body for a separate part earlier). This indeed does return something. Now it's time to try to make it filter on input. – L. Chu Dec 18 '16 at 21:20

0 Answers0