How to add Social Share buttons in Django?


We get to see Social Share buttons on most of the websites. They play an important role in ecommerce or any blogging or affiliate sites. As a web developer, you surely want people to like your website and want them to tell about your site to others on social media.

In this article, we will see how to make an automated website share social button.

Example

First of all, create a project and an app.

Install the django-social-share package −

pip install django-social-share

In settings.py, add django_social_share as an app in project.

INSTALLED_APPS += ['django_social_share']

In project's urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include('socialShare.urls')) #this is my app urls
]

Set up your app's urls.

In app's urls.py:, render a view.

from django.urls import path,include
from . import views
urlpatterns = [
   path('', views.home, name="home")
]

In views.py

from django.shortcuts import render

# Create your views here.
def home(request):
   return render(request,"home.html")

Here we simply rendered the frontend html.

Create a templates folder in app folder and create a "home.html" file with the following lines −

<!DOCTYPE html>
<html>
   <head>
      <title>TUT</title>
   </head>
   <body>
      {% load social_share %}
      {% post_to_facebook object_or_url "<p style='color: green;'>facebook</p>" %}
      {% post_to_linkedin object_or_url %}
      {% post_to_telegram "New Song: " object_or_url %}
   </body>
</html>

Here we created the frontend html. We loaded our library in frontend and then we used object_or_url after post_to_(social media name) to tell on which social media what to share, object_or_url to share website link.

You can add styles, classes, icons, any element you want! Now let's check the output and how it looks. 

Output

 


Updated on: 26-Aug-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements