Pergunta

I'm new to the Django and Python. I'm trying to create a Django app display on a page a streaming of Tweets from an account filtered by an ash tag (for the moment hardcoded).

That s my view.py

 from tweepy.streaming import StreamListener
 from tweepy import OAuthHandler
 from tweepy import Stream
 from django.http import HttpResponse

 consumer_key="XXXXX"
 consumer_secret="XXXXX"

 access_token="XXXXXX"
 access_token_secret="XXXXX"

class StdOutListener(StreamListener):

def on_data(self, data):
    print data
    return True

def on_error(self, status):
    print status

if __name__ == '__main__':
     l = StdOutListener()
     auth = OAuthHandler(consumer_key, consumer_secret)
     auth.set_access_token(access_token, access_token_secret)

stream = Stream(auth, l)
stream.filter(track=['BT'])


 def index(request):
  return HttpResponse("try")

I have obviously my consumer_key, consumer_secret, acess_token and acess_token_secret edited. In my urls.py I have

 from django.conf.urls import patterns, url
 from showTweets import views

 urlpatterns = patterns('',
     url(r'^$', views.index, name='index')
 )

And now ok if I go to the index is printing out my "try". But I d like to print out the StdOutListener class thats the one printing my tweets, I'm sure that is working because from terminal I go in that directory running

 python urls.py

it s displaying the tweets streaming on my terminal. But how do I display my streaming on my index page?

Foi útil?

Solução

As you are at the testing / practicing stage, your should use the internal django web server which can be accessed by:

python manage.py runserver

This will start up a server on port 8000 which you can access at http://127.0.0.1:8000. If the site isn't on your local machine simply add the IP address yu want the server to run on:

python manage.py runserver 192.168.1.1

I'm sorry if this sounds cheeky, but have you gone through the django tutorial at https://docs.djangoproject.com/en/1.5/? This is one of the first things that is mentioned in that tutorial, so if you haven't gone through it then you would benefit enormously from doing so.

Also you don't need the first line in that view (from django.db import models). I'd tell you why, but the docs explain it much better than I could.

Outras dicas

What's the function of this line of code. def index(request): return HttpResponse("try")

I think that's the problem. U set the index function to return 'try' => httpresponse is 'try'.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top