Skip to main content

django-instagram-profile

A Django application that syncs one or more users Instagram posts to your project's database and media storage.

  • Uses Instagram Basic Display API to fetch a users feed
  • Button in Django Admin to authenticate user
  • Supports multiple Instagram accounts
  • Stores long-lived access code in database to allow on-going sync without user interaction (NB: This code is currently in proof-of-concept status. Before using in production, you will probably want to update this field to be encrypted or stored somewhere else that is encrypted)
  • Management command for background sync with crontab (./manage.py sync_instagram_posts)

Instagram media is managed using Django admin console. Demo django project

Setup & configuration

1. Install django-instagram-profile

poetry add django-instagram-profile

or using Pip

pip install django-instagram-profile

Dependencies will be installed automatically

2. Create a Facebook/Instagram App

Follow this guide.

If your app is intended for the general public to authenticate, then you will need to publish your app. Otherwise, you can get away with leaving your app in developer mode and adding your users as testers.

To add a test user:

  • In the app admin, go Products > Basic Display > User Token Generator > Add or remove Instagram testers
  • In the users Instagram account login, go Settings > Apps and Websites > Tester Invites. Then accept the invite.

3. Add the application to INSTALLED_APPS:

INSTALLED_APPS = (
    # ...
    'instagram_profile',
)

4. Update your Django settings with your new app's client id, secret, & redirect url. Eg.

INSTAGRAM_PROFILE = {
    # You will get these from your registered instagram app
    'app_id': '123456789012345',
    'secret': '1234567890123456789012345678901234',
    'redirect_url': 'https://www.example.com/your-admin/instagram_profile/profile/authorized',        
}

OR if you prefer, create env.ini file inside your project directory with the following settings:

[instagram]
app_id = 123
secret = abc
redirect_url = https://www.example.com/your-admin/instagram_profile/profile/authorized

4. Migrate database

Run the database migrations

./manage.py migrate

5. Configure media (if you already haven't)

Depending on your storage backend and current project you may need to configure media.
Your project probably already has this configured.

Create a folder for Instagram media

/project_name/media/instagram

Configure correct paths for uploaded instagram media in settings.py

SITE_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'

Add media template context processor in the settings.py Also add template dir to the list of dirs

TEMPLATES = [
    {
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.media', # add this line
            ],
        },
    },
]

To serve media files during development need to add following lines to urls.py

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

6. Configure public views (optional)

django-instagram-profile comes with some public views to show your instagram posts. Depending on your project needs you may or may not want to use them.

If you want to utilise these them add url to display instagram media feed in urls.py:

urlpatterns = [
    # ...
    path('instagram/', include('instagram_profile.urls')), # add this line
]

Usage

Adding an account

If you have not publicly published your app, you will first need to setup your instagram user as a test user as described above.

Add a new profile in the Django admin, only setting the username field. (Instagram Sync > Profiles > Add)

After clicking "Save & Continue Editing", click "Authorize" in the top right of the profile form (depending on your needs, this might need to be done by your Instagram user). This will redirect you/them to Instagram. Accept all permissions.

Sync posts manually

Once you are authorized, you can sync posts by:

  • Clicking the "Sync" button on the Profile form. This will only sync this profile's posts.
  • Clicking the "Sync" button on the list of posts. This will sync posts for all profiles.

Sync posts in the background

Run this command. You'll probably want to set up a crontab (or similar) to run this regularly.

./manage.py sync_instagram_posts

The long-term access codes will be automatically renewed if they will expire within 30 days. So if this command is run more frequently than every 30 days, authentication should continue in-definitely.

Using post data in other ways

You can interface with the Post model however you like. We recommend the excellent django-imagekit library, which will allow you to resize and change these images for however you like in your own code.

Releases

  • 1.0.3 Merged pull request #19
  • 1.0.1 Merged pull requests #12, #13, #14, #15, #16
  • 1.0.0 Multi-account, django.contrib.storage, background-sync, and much more
  • 0.1.3 Use text field for instagram captions
  • 0.1.1 Handling invalid auth code exception
  • 0.1.0 Alpha version

Metadata

Release files for django-instagram-profile 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-instagram-profile 1.0.3
File Size Uploaded Provenance
django-instagram-profile-1.0.3.tar.gz 12.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-instagram-profile 1.0.3
File Interpreter ABI Platform
django_instagram_profile-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 29.3 kB

Release files / django-instagram-profile-1.0.3.tar.gz

Download URL django-instagram-profile-1.0.3.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b4580526069eab309676a08887d8906b32d3faea5881f62c2be4ff04fb02e655
BLAKE2b-256 checksum
How to use checksums
4f8104129cc0476bdeeb120a91dc0b4628c8d3e04505cd5c9aeb933eaa2ce2c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release files / django_instagram_profile-1.0.3-py3-none-any.whl

Download URL django_instagram_profile-1.0.3-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f136c1029c0645ed83317e2736a874f75d7790f8a7b2b98d5d41735710a2da9
BLAKE2b-256 checksum
How to use checksums
f4a9a4d60ca899d009983370fccc6a9b3a8fed27c869237c12f9e2886b17fed6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Security for django-instagram-profile 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page