0

Here's my html for my Django Twitter project. I am trying to embed tweets in Django

<html lang="en"> 
<h1>Tweets</h1>

<form action="" method="post">
    {% csrf_token %}
    {{ form }}
    <button type="submit">Submit</button>
</form>

<body> 
    {% if tweets %}
    <p>{{tweets}}<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></p>
    {% endif %}
</body> 
</html>

Here's my views.py:

import tweepy
from tweepy.auth import OAuthHandler
from .models import Tweet
from .models import Dates
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.shortcuts import render
from django.db import models, transaction
from django.db.models import Q
import os
import tweepy as tw
from .forms import TweetIDForm
from django import template

import requests

consumer_key = 'dddddd'
consumer_secret = 'cccccc'
access_token = 'dddddd'
access_token_secret = 'ccccc'

def clean_tweet_id(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = TweetIDForm(request.POST)
        print(form)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            tweet_id = form.cleaned_data.get("tweet_id")
            #act = form.cleaned_data.get("action") 
            auth = tw.OAuthHandler(consumer_key, consumer_secret)
            auth.set_access_token(access_token, access_token_secret)
            api = tw.API(auth, wait_on_rate_limit=True)
                            
            twtjson = requests.get('https://publish.twitter.com/oembed?url=' + tweet_id + '&omit_script=true')
            tweets = twtjson.json()
            tweet = tweets.get('html')   


        return render(request, 'tweet/tweet-list.html', {'form': form, 'tweet_id':tweet_id, 'tweets': tweet})

My code works so far, but for some reason it does not show the actual tweet, but it merely shows the HTML and not the actual tweet like it would be embedded on another site. I am trying to create an app where the user inputs the tweet URL and the tweet will be embedded itself. I worked for a couple days and it still does not work.

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
  • When you say that it doesn't work, what exactly do you see in the output where you expect the tweet to appear? – AKS Jun 30 '21 at 04:45
  • I meant that the tweet should be embed like it is on a website, but it merely showed the HTML in my webserver. – Vladimir George Jun 30 '21 at 04:45
  • 1
    Noticed "_I will update this question along the way when I need help._". Please don't do that unless the edit is regarding the current question only. If it is a new question ask it separately, it is [frowned upon when you keep changing your question](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions). – Abdul Aziz Barkat Jun 30 '21 at 04:48

1 Answers1

2

You see only the HTML and not the actual tweet because {{tweets}} will only render the escaped HTML. If you need to render that HTML you can use the safe filter:

{{tweets|safe}}

Please also check this SO Post: Rendering a template variable as HTML.

AKS
  • 18,983
  • 3
  • 43
  • 54