How to integrate MailChimp on Django to increase Subscribers
In this Post, we will discuss on how to integrate the MailChimp API to your Django Application. This API will help you to provide a platform to send emails to the audience who subscribes to your website, we will also discuss how to send a confirmation email for confirming the subscription using Threading because connecting with other API may take 2-3 second to connect. Using threads allows a program to run multiple operations concurrently in the same process space.
By MailChimp, you can send Beautiful Templated Message that will grow your Business by sending regular emails of your new product, news, post or whatever you want to promote. MailChimp is forever free but up to 2000 subscribers and 12,000 emails per month.
What is MailChimp
MailChimp is an email marketing service and their features and integrations allow sending marketing emails, automated messages, and targeted campaigns. MailChimp has been around since 2001 and now more than 15 million people and businesses around the world use MailChimp.
First, of fall you need to Sign up and create your account at MailChimp. Go to the MailChimp website to register your website.
Fill up your details or credentials of your and your website and finally activate your account from the mail they sent you.
Create a List
After complete registration with MailChimp, you will land to the Dashboard of MailChimp.
Now we have to create a List. So click on the button of Create a List shown below, or by going to the List Panel from the menu bar and there after clicking on Create a list Button.
Now fill up your all necessary details in List Form. These details may be included in the MailChimp emails you sent, so If you have any hosted email service, use that email address. After you successfully filled up your details, click on Save.
Get Your List and API KEY
After creating your List, get your List Key and API key.
You can get your List Key from lists > settings. Scroll down the page, you fill find a field of Unique id for list
After that, get your API key from Account > Extras > API Keys. Then click on Create a Key button to get your API key.
It is strongly recommended to hide your keys during the development of your Django Application. I would recommend you to save your keys in the settings.py file.
Setup in your Django Application
Now you have setup your Django Application to connect to the MailChimp API, whenever any user tries to Subscribe to your Website.
Like my previous articles, I will explain it by a brief example so that you can understand clearly.
So for the sake of example, I have created a table in models.py of two fields, first one is email_id the second one is timestamp.
You have to install a package of MailChimp. You can download from PyPI package mailchimp2.0.9 OR you can directly install with pip.
After installing the package, let’s actually set up the MailChimp Integration and sending a confirmation mail of subscription. Like discussed above, we used Threading to connect to MailChimp API because connecting with other API may take 2-3 second to connect. Using threads allows a program to run multiple operations concurrently in the same process space.
Note: This section of code, I have written by creating a new py file named utils.py in the same Django App. This is not necessary, but if you do, it will be good.
Let’s get set up the views. Usually, the Subscription forms are AJAX-powered and they are usually in Homepage. So in this tutorial, I have created an Ajax handling function based view.
Set up your URL.
Finally, you have to design the template for a Subscription form. Include this form in anywhere in your page, I have included this form in my footer.
Complete your form by adding ajax script, which will execute after submitting the email. Now you need to pass the csrftoken (Why?) as JSON object along with email id got from the input.
Now you are all set, you can test by subscribing with your known email. Check your subscribed email, you will get this type of mail.
This site is also powered by MailChimp. You can check by subscribing to our website. You will find our Subscribe Newsletter form available at the footer. If you have any problem regarding installation you can ask it in the Comment section.
comments powered by Disqus