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.

3
  • When you say that it doesn't work, what exactly do you see in the output where you expect the tweet to appear? Commented Jun 30, 2021 at 4:45
  • I meant that the tweet should be embed like it is on a website, but it merely showed the HTML in my webserver. Commented Jun 30, 2021 at 4: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. Commented Jun 30, 2021 at 4:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.