Reputation: 11
pretty new to flask and ajax and was wondering if i could get some help. I am using python-twitter and want to be able to type in a search parameter that will search twitter for the requested word/words.
I have tried multiple solutions and both my friend and i can't see where it is going wrong. I can see the search variable being stored in ajax through a console.log but i can't get it to transfer that variable over to the twitter function that will search for the variable.
Here is my ajax:
$('#form').on('submit', function(e){
var search = $('#search').val();
e.preventDefault();
$.ajax({
url: '/twitter',
data: {'search': search},
method: 'Get',
success: function(result) {
// twit(data)
console.log(search)
}
});
});
And my Twit() As requested
@app.route('/twitter', methods=['GET', 'POST'])
def twit():
tempo = request.form['search']
searched = "Yes"
searched = request.args.get('search1')
# searched = request.form.get('number')
print(searched, file=sys.stderr)
api = twitter.Api(\
consumer_key='TWVxfrliliibQxjNWz4tAlDIj',
consumer_secret='QK2IBqNorysgD3quaBJVCMDdMApOpo5fW5g6Pl4Di97ToRjsGy',
access_token_key='3011394611-QmeEQ5yaL6OJTOmqemXV3eS0DpE09P0sy64XhnL',
access_token_secret='tEMygLkKBDwfVepHAg7G9BI7N8VQJM7U9KKefpV40jUq1'
)
count = "10"
data = api.GetSearch(
raw_query= 'q='+str(searched)+'&'
'count='+str(count)+'&'
'lang=en&'
'tweet_mode=extended&'
#'geocode=51.4027029,-0.3040634,10mi' #kington
)
print(data)
for i in data:
print('---------------------------')
if hasattr(i, 'retweeted_status'):
print(i.user.name + ': ' + i.full_text)
else:
print(i.user.name + ': ' + i.retweeted_status.full_text)
return(render_template('Twitter.html', d=data))
if __name__ == '__main__':
app.run(debug=True)
Here is the form
<form method="get" id="form">
<label for="search">Enter Search Parameter : </label>
<input type="text" id="search" name="search" autofocus autocomplete="off">
<button>Post</button>
</form>
The page should be updated using the searched for term.
Apologies if there is a very obvious mistake
Upvotes: 0
Views: 171