An app to manage tweets in a Django project
Project description
django-tweets
Create and delete tweets in a Django project.
This packages takes the advantage of the tweepy functionalies to connect it to a Django Backend.
The tweets objects can have media files as well.
Set up
Twitter Account
- Make sure you have a Twitter account.
- Go to the Twitter Developer Portal.
- Create a Project and an App
- Make sure your App has read and write permissions.
- Genenerate the necessary secrets and token.
django-tweets
- Install from PyPI
python -m pip install django-tweets
Add the package to your settins INSTALLED_APPS
INSTALLED_APPS = [
...
"django_tweets",
...
]
Add the following settings to your Django project.
Django setting | Description | Required |
---|---|---|
TWITTER_API_KEY | Twitter API OAuth 1.0a Consumer Key | Yes |
TWITTER_API_KEY_SECRET | Twitter API OAuth 1.0a Consumer Secret | Yes |
TWITTER_BEARER_TOKEN | Twitter API OAuth 2.0 Bearer Token / Access Token | Yes |
TWITTER_ACCESS_TOKEN | Twitter API OAuth 1.0a Access Token | Yes |
TWITTER_ACCESS_TOKEN_SECRET | Twitter API OAuth 1.0a Access Token Secret | Yes |
DJANGO_TWEETS_SYNC_DELETE | Synchronize object deletion with Twitter API. This is activated by default. | No |
Example:
import os
from dotenv import load_dotenv
load_dotenv()
...
# django-tweets
# Consumer Keys
TWITTER_API_KEY = os.environ.get("TWITTER_API_KEY")
TWITTER_API_KEY_SECRET = os.environ.get("TWITTER_API_KEY_SECRET")
# Authentication Tokens
TWITTER_BEARER_TOKEN = os.environ.get("TWITTER_BEARER_TOKEN")
TWITTER_ACCESS_TOKEN = os.environ.get("TWITTER_ACCESS_TOKEN")
TWITTER_ACCESS_TOKEN_SECRET = os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")
# OAuth 2.0 Client ID and Client Secret
TWITTER_CLIENT_ID = os.environ.get("TWITTER_CLIENT_ID")
TWITTER_CLIENT_SECRET = os.environ.get("TWITTER_CLIENT_SECRET")
Usage
Create a simple tweet
from django_tweets.models import Tweet
# create a tweet in the db
tweet = Tweet.objects.create(text="Hi, this is my tweet using django-tweets and tweepy")
# publish it
tweet.publish()
Create a simple tweet with a media file
from pathlib import Path
from django.core.files import File
from django_tweets.models import Tweet, MediaFile
# create a media file
path = Path("path/to/my/file.jpg")
with path.open(mode="rb") as f:
mediafile = MediaFile.objects.create(
title="nice photo",
file=File(f, name=path.name)
)
# create a tweet in the db
tweet = Tweet.objects.create(text="Hi, this is my tweet using django-tweets and tweepy")
## add the media file to the tweet object
tweet.files.add(mediafile)
# publish it
tweet.publish()
Questions
- How Can I get the read and write permission for my app? Answer:
- In the Twitter Developer Portal, click on gear icon of the project app
- Then go User authentication settings and click on the Edit button.
- Configure the the form and submit.
About
©Django is a registered trademark of the Django Software Foundation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-tweets-0.0.2.tar.gz
(6.3 kB
view hashes)
Built Distribution
Close
Hashes for django_tweets-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbdb4acab2521b90bf768a10b0d6b29b0f5243efcb4eca9fc3f6e2834a4052c1 |
|
MD5 | c3ec9501cd4ddaaa3f5b0bcf78ac7977 |
|
BLAKE2b-256 | ddf363c62e2690559ce5432ee6eeecc17da376f2354bc6c9bbf7e46deec7153a |