Ive created my own dictionary from the tweepy api results.
it displays the data I want on the frontend. However, how do I go about looping over my dictionary so html is produced for each dictionary created.
I also wish to have user input determine the query parameter but not sure how do it
This is what I've tried but no avail..
{% for tweet in twitter_data %}
<div class="tweet-container">
<img src="{{twitter_data.profile_dp}}" alt="avatar" class="avatar" />
<time class="tweet-time">{{twitter_data.created_at}}</time>
<div class="push">
<div class="user">
<div class="user-string"><span class="name">{{twitter_data.user_name}}</span><span
class="username">@{{twitter_data.user_screen_name}}</span></div>
</div>
<blockquote class="tweet"><span class="at">@lorem_bot </span>{{twitter_data.text}} <span
class="tag">#lorem2015</span></blockquote>
<div class="icons">
<i class="fa fa-reply"><span class="icon-number"> 10</span></i>
<i class="fa fa-retweet"><span class="icon-number"> {{twitter_data.retweet_count}}</span></i>
<i class="fa fa-star"><span class="icon-number"> {{twitter_data.fav_count}}</span></i>
<i class="fa fa-ellipsis-h"></i>
<i class="fa fa-eye"></i>
</div>
</div>
</div>
{% end for %}
from django.shortcuts import render
import requests
import tweepy
import json
# Create your views here.
CONSUMER_KEY = 'm'
CONSUMER_SECRET = 'Xu'
ACCESS_KEY = '116'
ACCESS_SECRET = 'wz'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
def index(requests):
for tweet in tweepy.Cursor(api.search,
query="xbox",
q="{xbox}",
count=1,
result_type="recent",
include_entities=True,
lang="en").items():
twitter_data = {
'text': tweet.text,
'user_name': tweet.user.name,
'user_screen_name': tweet.user.screen_name,
'created_at': tweet.created_at,
'profile_dp': tweet.user.profile_image_url,
'retweet_count': tweet.retweet_count,
'fav_count': tweet.favorite_count
}
context = {'twitter_data': twitter_data}
return render(requests, 'twitter/twitter.html', context)
You don't need to run multiple loops in your template. You only need to save those tweet objects as a list in your view and then loop over them in the template.
#views.py
tweets = tweepy.Cursor(api.search,
query="xbox",
q="{xbox}",
count=1,
result_type="recent",
include_entities=True,
lang="en").items()
context = {'tweets': tweets}
Then in your template:
{% for tweet in tweets %}
{{ tweet.text }}
{{ tweet.user.name }}
{{ tweet.retweet_count }}
{% endfor %}
etc....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.