0

my views.py :

def responded(request):
  reply_twt = Reply_twt.objects.all() 
  for tweet in reply_twt:
    rply_tweet = home_timeline.api.get_status(tweet.tweetid)
  return render (request, 'analytics/responded.html', {'rply_tweet': rply_tweet})

my html:

{% extends 'analytics/header.html' %}
{% block body %}

        {% for tweet in rply_tweet %}
            {{tweet.text}}
        {% endfor %}
{% endblock %}

i need to print multiple tweet.text in for loop and even if i send data to html page it is only data on single tweet. how to resolve this . Thank You in advance

3
  • 1
    rply_tweet is not a list of twwets, it will have the latest tweet, since you constantly overwrite it. Commented Jul 19, 2018 at 11:57
  • then how do i solve this ? Commented Jul 19, 2018 at 11:58
  • 1
    rply_tweet = [home_timeline.api.get_status(tweet.tweetid) for tweet in reply_twt] Commented Jul 19, 2018 at 12:00

2 Answers 2

1

You write:

for tweet in reply_twt:
    rply_tweet = home_timeline.api.get_status(tweet.tweetid)

This means after every iteration you will overwrite rply_tweet with the response of the new iteration. But your template seems to suggest that you want to render all the responses (furthermore it would otherwise only waste resources since you never use the previous responses).

You can thus for example construct a list with all the responses with list comprehension:

rply_tweet = [home_timeline.api.get_status(tweet.tweetid) for tweet in reply_twt]

That being said, if your Reply_twt stores all the information of the tweet (or at least the data you want to render), you better simply use these objects:

# in case a Reply_twt contains *all* the necessary data
def responded(request):
    reply_twt = Reply_twt.objects.all()
    return render (request, 'analytics/responded.html', {'rply_tweet': reply_twt })
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much you saved my day
1

Just return the full list of tweets

views.py

def responded(request):
  reply_twt = Reply_twt.objects.all() 
  return render (request, 'analytics/responded.html', {'rply_tweet': list(reply_twt}))

3 Comments

It is not really clear if the tweet (with all the elements) is stored in the database. +1 nevertheless :)
I wasn't sure what was intended to be honest.. since I thought it was the tweet objects that are desired (tweet.text is used), but, indeed you are also right.
well I find it a bit weird that apparently we have the id, and probably other data as well, but then use the API to fetch the tweet. A good reason could be to check if the tweet has been modified meanwhile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.