2017-06-25 2 views
0

Hmmmm, cela peut être une tâche facile mais peut être complexe. J'ai un problème à mettre en place un flux Twitter SEO friendly dans le framework Django. La plupart des charges lourdes est fait comme une balise modèle de fichier blog_tags.py comme suit:Légère émission d'analyse de texte en html

@register.inclusion_tag('blog/frame/twitter.html') 
def show_latest_tweets(): 
    tweets = [] 

    try: 
     """The import error is here to catch any server migrations were the tweepy package not to be found in site_packages""" 
     import tweepy 
    except: 
     tweets.append({'status': 'There was a problem referencing our tweets. Please inform our webmaster.', 'relative_date': 'Just now'}) 
     raise ImportError 

    # OAuth process, using the keys and tokens 
    auth = tweepy.OAuthHandler(settings.EOS_TWITTER_FEED['EOS_FEED_TWITTER_KEY'], settings.EOS_TWITTER_FEED['EOS_FEED_TWITTER_SECRET']) 
    auth.set_access_token(settings.EOS_TWITTER_FEED['EOS_FEED_TWITTER_ACCESS_TOKEN'], settings.EOS_TWITTER_FEED['EOS_FEED_TWITTER_ACCESS_TOKEN_SECRET']) 

    # Creation of the actual interface, using authentication 
    api = tweepy.API(auth) 

    user = 'FFXVEN' 

    avatar_url = api.get_user(screen_name='@'+user).profile_image_url 

    i = 0 
    for tweet in tweepy.Cursor(api.user_timeline, screen_name='@'+user).items(): 
     if 'RT' not in tweet.text: 
      if i <= 4: 
       status = tweet.text 
       hashtags = [word for word in status.split() if word[0] == "#"] 

       #Find hashtags in tweet and create a string to contain <a href="https://twitter.com/search?q=" + hashtag>#hashtag</a> 
       for hashtag in hashtags: 
        if hashtag.endswith((',',';','.')): 
         hashtag = hashtag[:-1] 

        status = status.replace(hashtag, '<a href="https://twitter.com/search?q={}">{}</a>'.format(hashtag[1:], hashtag)) 

       relative_date = tweet.created_at 
       tweets.append({'user': user, 'avatar_url': avatar_url, 'status': html.unescape(status), 'relative_date': relative_date}) 
       i += 1 
      else: 
       break 
     else: 
      continue 

    return { "tweets": tweets } 

Tout fonctionne bien - il retourne tout ce que je veux et actuellement il déshabille les hashtags et de les remplacer par Twitter amical hashtags. CEPENDANT, côté client c'est ce qui est affiché:

enter image description here

Je suis coincé sur la façon dont je travaillerais il tel que côté client les éléments HTML sont rendus comme des éléments html et pas du texte préformaté?

+0

montrer votre modèle où vous affichez – Exprator

+0

standard Tweet en boucle tweets avec {{}} tweet.status variable affichée dans une balise

. –

Répondre

3
{{ tweet.status|safe } 

essayer le filtre de modèle sûr

+0

Awesome - va essayer ça. –