0

I'm working a college project which basically requires building a simple news website to fetch tweets from Twitter. I chose Tweepy as my Python library for this task.

To achieve this news website, I have a table called 'tweet_source' which stores Twitter usernames. In a view called 'tweets.html', I first fetch each username from 'tweet_source' and pass it to the Tweepy Python script (which contains my access token,etc.)

The Tweepy script will fetch data from Twitter such as first_name, tweets, profile image and generate divs for each user.

Everything works very well and it does generate the divs neatly. But, our main criteria is to have a "Refresh" button which will fetch new tweets from Twitter using AJAX.

In my case - 1. Re-run the Python script 2. Re-generate the divs.

I tried one approach using the default AJAX method where I created another view with the script between {{ }} and used that as response. But, the generated divs were messed up and overlapped one another.

I came across another approach which involves just refreshing the current page using jQuery's AJAX method because in this way - 1. The script will re-run 2. divs re-generated exactly the way I want.

function refresh_tweets(){ 
                     $.ajax({
     type:'post',
     data:{},
     url:'tweets.html',
     dataType:'html',
     success:function(response){
       $('body').html(response);
     }
    });
}   

Now, the problem is that the script runs (I can see the output on my web2py console) but there are no divs created on the page i.e; nothing is displayed except a few headings.

NOTE :- It might be worth mentioning that the tweets that I do fetch from Twitter using my Tweepy script are stored in another table in the database. (Project guidelines asked us to do this). To achieve this I have added db.tweet_tweet.truncate() at the top of my page to make sure that I delete old tweets and add the new ones.

I would appreciate if someone can help me with this. Or offer an easier solution to this task.

I would be happy to share the code of 'tweets.html' if required.

Thanks, Arjun

EDIT: The 'tweet_tweet' table which stores the tweets does NOT become empty. I just tested it now and suppose I click on "Refresh", a new tweet DOES get added to the table successfully but once again there are no divs nor output generated.

Arjun
  • 817
  • 3
  • 16
  • 28

1 Answers1

0

I solved my problem by using web2py's AJAX method.

ajax(url, [name1, name2, ...], target)

It was mainly an issue with re-generating the divs in 'tweets.html'.

Arjun

Arjun
  • 817
  • 3
  • 16
  • 28