简体   繁体   English

如何循环字典以在Django的前端显示并实现用户输入

[英]How to loop over dictionary to display in frontend in django and implement user input

Ive created my own dictionary from the tweepy api results. 我已经从tweepy api结果创建了自己的字典。

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. 但是,我该如何遍历字典,以便为创建的每个字典生成html。

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. 您只需要在视图中将这些tweet对象保存为列表,然后在模板中对其进行循环。

#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.... 等等....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM