<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anurag Rana</title>
    <description>The latest articles on DEV Community by Anurag Rana (@anuragrana).</description>
    <link>https://dev.to/anuragrana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F185955%2F7c1ea56e-8228-4169-a1d8-0fb71dc778ea.jpeg</url>
      <title>DEV Community: Anurag Rana</title>
      <link>https://dev.to/anuragrana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anuragrana"/>
    <language>en</language>
    <item>
      <title>How to send emails using Python and SMTP server</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Thu, 16 May 2024 12:32:11 +0000</pubDate>
      <link>https://dev.to/anuragrana/how-to-send-emails-using-python-and-smtp-server-37ki</link>
      <guid>https://dev.to/anuragrana/how-to-send-emails-using-python-and-smtp-server-37ki</guid>
      <description>&lt;p&gt;One way to send emails is by using third-party APIs. Another way is to connect to the SMTP server directly. This is the second approach here.&lt;/p&gt;

&lt;p&gt;Import the required libraries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import email.utils as utils
import time
import ssl
from cryptography.fernet import Fernet
from urllib.parse import quote, unquote
from datetime import datetime
import html2text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create the &lt;code&gt;EmailSenderclass&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;__init__&lt;/code&gt; and connectmethods as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EmailSender:
    def __init__(self):
        self.smtp_server = "IP or DNS of SMTP Server"
        self.smtp_port = PORT HERE. Probabily 587
        self.username = "USERNAME"
        self.password = "PASSWORD"
        self.sender_name = "ANURAG RANA"
        self.sender_address = "admin@anuragrana.in"
        self.domain = "http://anuragrana.in"
        self.smtp_connection = None

   def connect(self):
        if not self.smtp_connection:
            # this line below takes the most time
            self.smtp_connection = smtplib.SMTP(self.smtp_server, self.smtp_port)
            # Create a secure SSL context
            context = ssl.create_default_context()
            # self.smtp_connection.starttls(context=context) # might generate SSL error
            self.smtp_connection.starttls()
            self.smtp_connection.login(self.username, self.password)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;send&lt;/code&gt; method to this class to send the email.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def send_email(self, to_address, subject, body_html):
    msg = MIMEMultipart("alternative")
    msg['message-id'] = utils.make_msgid(domain=self.domain)
    print(msg['message-id'])
    msg["Subject"] = subject
    msg["From"] = f"{self.sender_name} &amp;lt;{self.sender_address}&amp;gt;"
    msg["To"] = to_address
    # Attach the body of the email - text plain
    text_content = html2text.html2text(body_html)
    msg.attach(MIMEText(text_content, "plain"))
    # Attach the body of the email - html
    msg.attach(MIMEText(body_html, "html"))
    # Add the List-Unsubscribe header
    msg.add_header('List-Unsubscribe', unsubscribe_url)
    msg["Date"] = utils.formatdate(localtime=True)
    try:
        response = self.smtp_connection.sendmail(self.sender_address, to_address, msg.as_string())
        print(response)
    except Exception as e:
        print(e)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; message-id, List-Unsubscribe, and Date are three headers that are required for better deliverability. Without these headers, your email score will be low. A low email score might lead your email to the spam folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MIMEMultipart(“alternative”):&lt;/strong&gt; This is a subtype of the multipart content type. Using “alternative” indicates that the email message contains multiple representations of the same content, but in different formats. For example, you might have the same email message in both plain text and HTML. The recipient’s email client can then choose which one to display, usually the last part that the client understands. This is useful for ensuring that the recipient can read the email regardless of whether their email client supports plain text or HTML.&lt;/p&gt;

&lt;p&gt;Finally, add the close-connection method to the above class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
# close the connection
def close_connection(self):
    if self.smtp_connection:
        self.smtp_connection.quit()
        self.smtp_connection = None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in the main code, perform the below steps to send the emails.&lt;/p&gt;

&lt;p&gt;Initialize the EmailSender class and call the connect method to create a connection with the SMTP server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;email_sender = EmailSender()
email_sender.connect()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an Email Template in an HTML file. Read the content of this file in a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Read the content of the HTML file
with open("templates/my-first-email-template.html", "r", encoding="utf-8") as file:
    body = file.read()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create the list of recipients. Let’s call it batch. Iterate over this batch to send emails to everyone individually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;batch = []
# current_batch text file contains email, one email in each line
with open("current_batch.txt", 'r') as file:
    batch = file.readlines()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now send the emails.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
for recipient in batch:
    # sleep for few seconds before each email to avoid flooding the recepient's inbox
    time.sleep(5)
    if not recipient or not recipient.strip():
        continue
    recipient = recipient.strip()
    # check if recipient is in bouncelog
    if recipient in bounced:
        print(f'email in bouncelog. {recipient}')
        continue

    email_sender.send_email(recipient, subject, body)
    print(f'Mail sent to {recipient} - {time.time()}')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;We will discuss the bounce log and how to parse the POSTFIX logs for bounced emails in another article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connect with me if you want to set up a fully functional new email Postfix SMTP server to send and receive emails.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://anuragrana.in"&gt;https://anuragrana.in&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me on Medium: &lt;a href="https://medium.com/@03A_R/custom-parameter-logging-in-postfix-mta-logs-30c269c681b2"&gt;https://medium.com/@03A_R/custom-parameter-logging-in-postfix-mta-logs-30c269c681b2&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postfix</category>
      <category>smtp</category>
      <category>email</category>
      <category>python</category>
    </item>
    <item>
      <title>Using Git Credentials Manager (GCM)</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Mon, 28 Nov 2022 20:12:56 +0000</pubDate>
      <link>https://dev.to/anuragrana/using-git-credentials-manager-gcm-1hi0</link>
      <guid>https://dev.to/anuragrana/using-git-credentials-manager-gcm-1hi0</guid>
      <description>&lt;p&gt;Origianally published at &lt;a href="https://pythoncircle.com/post/765/avoid-typing-git-credentials-everytime-use-git-credentials-manager/"&gt;PythonCircle.Com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Are you tired of typing the Git username and password 512 times a day?&lt;/p&gt;

&lt;p&gt;Use Git Credential Manager (GCM). This supports all major version control tools, GitLab, GitHub, Bitbucket, etc.&lt;/p&gt;

&lt;p&gt;Here are the steps to set up GCM.&lt;/p&gt;

&lt;p&gt;1) &lt;/p&gt;

&lt;p&gt;Download the GCM Deb file from here - &lt;a href="https://github.com/GitCredentialManager/git-credential-manager/releases/download/v2.0.877/gcm-linux_amd64.2.0.877.deb"&gt;https://github.com/GitCredentialManager/git-credential-manager/releases/download/v2.0.877/gcm-linux_amd64.2.0.877.deb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2)&lt;/p&gt;

&lt;p&gt;Install it using the below command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo dpkg -i &amp;lt;path-to-package&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Configure it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git-credential-manager configure&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3)&lt;/p&gt;

&lt;p&gt;Go to your .bashrc file and copy-paste this line at the bottom.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export GCM_CREDENTIAL_STORE=plaintext&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Restart the terminal.&lt;/p&gt;

&lt;p&gt;Now you have to type the credentials just once and they will be stored on your system.&lt;/p&gt;

&lt;p&gt;Note: There are many other ways to store the credentials. Storing credentials in plain text format is &lt;strong&gt;unsafe&lt;/strong&gt; but convenient. This does not require any more packages to work.&lt;/p&gt;

&lt;p&gt;You may explore more ways to store the credentials here &lt;a href="https://github.com/GitCredentialManager/git-credential-manager/blob/release/docs/install.md"&gt;https://github.com/GitCredentialManager/git-credential-manager/blob/release/docs/install.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read More:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/GitCredentialManager/git-credential-manager/blob/main/README.md"&gt;https://github.com/GitCredentialManager/git-credential-manager/blob/main/README.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/GitCredentialManager/git-credential-manager/blob/release/docs/install.md"&gt;https://github.com/GitCredentialManager/git-credential-manager/blob/release/docs/install.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/GitCredentialManager/git-credential-manager/releases/tag/v2.0.877"&gt;https://github.com/GitCredentialManager/git-credential-manager/releases/tag/v2.0.877&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>credentials</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Hello World in Django 2.2</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Sat, 15 Feb 2020 15:56:06 +0000</pubDate>
      <link>https://dev.to/anuragrana/hello-world-in-django-2-2-2a9d</link>
      <guid>https://dev.to/anuragrana/hello-world-in-django-2-2-2a9d</guid>
      <description>&lt;p&gt;We have already covered how to &lt;a href="https://www.pythoncircle.com/post/26/hello-word-in-django-how-to-start-with-django/"&gt;start with Django 1.9&lt;/a&gt;. In this article, we will see how to quickly get a simple page running using Django 2.2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pythoncircle.com/post/404/virtual-environment-in-python-a-pocket-guide/"&gt;Create a virtual environment&lt;/a&gt; using python3. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;virtualenv -p /use/bin/python3.5 venv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Activate the virtual environment.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;source venv/bin/activate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now install the Django2.2 inside this virtual environment using pip.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install Django==2.2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once Django has been installed, run the &lt;code&gt;django-admin&lt;/code&gt; command to start a project.&lt;/p&gt;

&lt;p&gt;Create your project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ django-admin startproject helloworld&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Go inside the newly created project directory. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ cd helloworld&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will see a file manage.py and a directory with the same name as of your project.&lt;/p&gt;

&lt;p&gt;Create an app here. Every project consists of one or more apps. These are plug-able modules that can be reused in another project if written properly. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ python manage.py startapp hw&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Go inside hw directory. create a new file urls.py.&lt;/p&gt;

&lt;p&gt;Add the below lines to this file and save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;.&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;

&lt;span class="n"&gt;app_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hw"&lt;/span&gt;
&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;r''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'home'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Open views.py file and save the below code in it.  This will create the first view function which will be called when hitting base project URL i.e. localhost:8000/ on the local server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.shortcuts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;


&lt;span class="c1"&gt;# Create your views here.
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;home&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'hw/home.html'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Include the hw app's URLs in the main project URLs file.&lt;/p&gt;

&lt;p&gt;Open helloworld/urls.py file and add below line in urlpatterns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'admin/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;r''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'hw.urls'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"hw"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now finally add hw in installed apps in helloworld/settings.py file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;INSTALLED_APPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.admin'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.auth'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.contenttypes'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.sessions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.messages'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.staticfiles'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'hw'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now start the Django development server by running the command. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ python manage.py runserver&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will run the python HTTP server on the localhost and 8000 port. If you want to run it on a different port, use port number in the command. For example &lt;code&gt;$ python manage.py runserver 8888&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Use the below command to make your project available for everyone on the network.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ python manage.py runserver 0.0.0.0:8000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open the web browser and got to URL localhost:8000 and you will be able to see the home page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source code:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The source code is available on &lt;a href="https://github.com/anuragrana/Helloworld-Django2.2"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Host your Django Application for free on &lt;a href="https://www.pythoncircle.com/post/18/how-to-host-django-app-on-pythonanywhere-for-free/"&gt;PythonAnyWhere&lt;/a&gt;. If you want full control of your application and server, you should consider &lt;a href="https://m.do.co/c/4dd631f4a322"&gt;DigitalOcean&lt;/a&gt;. Create an account with this &lt;a href="https://m.do.co/c/4dd631f4a322"&gt;link&lt;/a&gt; and get $100 credits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>django</category>
    </item>
    <item>
      <title>AWS EC2 Vs PythonAnyWhere Vs DigitalOcean for hosting Django application</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Fri, 07 Feb 2020 08:57:59 +0000</pubDate>
      <link>https://dev.to/anuragrana/aws-ec2-vs-pythonanywhere-vs-digitalocean-for-hosting-django-application-4dfm</link>
      <guid>https://dev.to/anuragrana/aws-ec2-vs-pythonanywhere-vs-digitalocean-for-hosting-django-application-4dfm</guid>
      <description>&lt;p&gt;We have hosted Django applications on AWS EC2, PythonAnyWhere and now on DigitalOcean as well. Here is a brief comparison of all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS EC2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Offers pay-as-you-go compute capacity
&lt;/li&gt;
&lt;li&gt;This is a large service that provides resizable compute capacity
&lt;/li&gt;
&lt;li&gt;Free for 12 months (750 hours of CPU usage)
&lt;/li&gt;
&lt;li&gt;Can use a custom domain in free tier plan (Extra charges may apply)
&lt;/li&gt;
&lt;li&gt;SSH access available- Browser-based SSH connection available
&lt;/li&gt;
&lt;li&gt;The developer needs to set up everything from scratch. Intermediate level of expertise required
&lt;/li&gt;
&lt;li&gt;Root level access available
&lt;/li&gt;
&lt;li&gt;Can install and use whatever tool/software is required
&lt;/li&gt;
&lt;li&gt;Highly scalable
&lt;/li&gt;
&lt;li&gt;The developer needs to manage the webserver
&lt;/li&gt;
&lt;li&gt;Pricing &lt;a href="https://aws.amazon.com/ec2/pricing/"&gt;https://aws.amazon.com/ec2/pricing/&lt;/a&gt;
- &lt;a href="https://aws.amazon.com/free/?"&gt;Create a free account &lt;/a&gt;
- &lt;a href="https://www.pythoncircle.com/post/697/hosting-django-app-for-free-on-amazon-aws-ec2-with-gunicorn-and-nginx/"&gt;Hosting the Django app on EC2&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Easiness to use: 5/10
&lt;/li&gt;
&lt;li&gt;Control over application and server: 10/10&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;PythonAnyWhere:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Offers fixed price shared hosting starting with $5 per month
&lt;/li&gt;
&lt;li&gt;Small Paas (platform as a service) provider which supports only Python web apps
&lt;/li&gt;
&lt;li&gt;Always free plan available. 100 CPU seconds per day. 512 MB space
&lt;/li&gt;
&lt;li&gt;Can not use a custom domain. The app will be hosted on username.pythonanywhere.com
&lt;/li&gt;
&lt;li&gt;SSH access available
&lt;/li&gt;
&lt;li&gt;Browser-based terminals available
&lt;/li&gt;
&lt;li&gt;The application setup is extremely easy.
&lt;/li&gt;
&lt;li&gt;Root level access not available
&lt;/li&gt;
&lt;li&gt;Apart from web hosting, Only MySQL, Postgres and schedule tasks support is provided
&lt;/li&gt;
&lt;li&gt;Good for small to medium traffic apps
&lt;/li&gt;
&lt;li&gt;The developer needs to take care of code
&lt;/li&gt;
&lt;li&gt;Pricing &lt;a href="https://www.pythonanywhere.com/pricing/"&gt;https://www.pythonanywhere.com/pricing/&lt;/a&gt;
- &lt;a href="https://www.pythonanywhere.com/?affiliate_id=000f9990"&gt;Create a free account&lt;/a&gt;
- &lt;a href="https://www.pythoncircle.com/post/18/how-to-host-django-app-on-pythonanywhere-for-free/"&gt;Hosting the Django app on PythonAnyWhere&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Easiness to use: 9/10
&lt;/li&gt;
&lt;li&gt;Control over application and server: 5/10
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Digital Ocean:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Offers pay-as-you-go compute capacity capped at the fixed price starting with $5/month.
&lt;/li&gt;
&lt;li&gt;This is a small service that provides resizable compute capacity
&lt;/li&gt;
&lt;li&gt;Free $100 credit if the &lt;a href="https://m.do.co/c/4dd631f4a322"&gt;account&lt;/a&gt; is created using &lt;a href="https://m.do.co/c/4dd631f4a322"&gt;this link&lt;/a&gt;.
&lt;/li&gt;
&lt;li&gt;Simple and easy interface to manage security and billing.
&lt;/li&gt;
&lt;li&gt;Relatively easier to use than AWS EC2 and better control over application and system if compared with PythonAnyWhere.
&lt;/li&gt;
&lt;li&gt;We can use a custom domain.
&lt;/li&gt;
&lt;li&gt;SSH access available.- No browser-based SSH connection available
&lt;/li&gt;
&lt;li&gt;The developer needs to set up everything from scratch. Intermediate level of expertise required.
&lt;/li&gt;
&lt;li&gt;Root level access available
&lt;/li&gt;
&lt;li&gt;Can install and use whatever tool/software is required
&lt;/li&gt;
&lt;li&gt;Highly scalable
&lt;/li&gt;
&lt;li&gt;The developer needs to manage the webserver.
&lt;/li&gt;
&lt;li&gt;A floating IP address is given free of cost.
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.digitalocean.com/pricing/"&gt;Pricing&lt;/a&gt;
- &lt;a href="https://m.do.co/c/4dd631f4a322"&gt;Create a free account &lt;/a&gt;
- Hosting the Django app on Digital Ocean (article coming soon)
&lt;/li&gt;
&lt;li&gt;Easiness to use: 9/10
&lt;/li&gt;
&lt;li&gt;Control over application and server: 9/10
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to add points, please comment below.&lt;/p&gt;

&lt;p&gt;So far my favorite is DigitalOcean. If you have a small application to host without requiring anything except MySQL, go for PythonAnyWhere else go for DigitalOcean. &lt;/p&gt;

&lt;p&gt;If you need any help in setting up your application on DigitalOcean or PythonAnyWhere, feel free to reach out to us.&lt;/p&gt;

&lt;p&gt;Host your Django Application for free on &lt;a href="https://www.pythoncircle.com/post/18/how-to-host-django-app-on-pythonanywhere-for-free/"&gt;PythonAnyWhere&lt;/a&gt;. If you want the full control on your application and server, you should consider &lt;a href="https://m.do.co/c/4dd631f4a322"&gt;DigitalOcean&lt;/a&gt;. Create an account with &lt;a href="https://m.do.co/c/4dd631f4a322"&gt;this link&lt;/a&gt; and get $100 credits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>django</category>
    </item>
    <item>
      <title>PythonAnyWhere Vs AWS EC2 for Django hosting</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Mon, 20 Jan 2020 19:14:58 +0000</pubDate>
      <link>https://dev.to/anuragrana/pythonanywhere-vs-aws-ec2-for-django-hosting-54ng</link>
      <guid>https://dev.to/anuragrana/pythonanywhere-vs-aws-ec2-for-django-hosting-54ng</guid>
      <description>&lt;p&gt;We have hosted Django applications on AWS EC2 as well as on PythonAnyWhere. Here is a brief comparison of both.&lt;/p&gt;

&lt;p&gt;AWS EC2:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Offers pay-as-you-go compute capacity&lt;/li&gt;
&lt;li&gt;This is a small service that provides resizable compute capacity&lt;/li&gt;
&lt;li&gt;Free for 12 months (750 hours of CPU usage)&lt;/li&gt;
&lt;li&gt;Can use a custom domain in free tier plan (Extra charges may apply)&lt;/li&gt;
&lt;li&gt;SSH access available&lt;/li&gt;
&lt;li&gt;Browser-based SSH connection available&lt;/li&gt;
&lt;li&gt;The developer needs to set up everything from scratch. Intermediate level of expertise required&lt;/li&gt;
&lt;li&gt;Root level access available&lt;/li&gt;
&lt;li&gt;Can install and use whatever tool/software is required&lt;/li&gt;
&lt;li&gt;Highly scalable&lt;/li&gt;
&lt;li&gt;The developer needs to manage the webserver&lt;/li&gt;
&lt;li&gt;Pricing &lt;a href="https://aws.amazon.com/ec2/pricing/"&gt;https://aws.amazon.com/ec2/pricing/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/free/"&gt;Create a free account&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pythoncircle.com/post/697/hosting-django-app-for-free-on-amazon-aws-ec2-with-gunicorn-and-nginx/"&gt;Hosting the Django app on EC2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PythonAnyWhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Offers fixed price shared hosting starting with $5 per month&lt;/li&gt;
&lt;li&gt;Small Paas (platform as a service) provider which supports only Python web apps&lt;/li&gt;
&lt;li&gt;Always free plan available. 100 CPU seconds per day. 512 MB space&lt;/li&gt;
&lt;li&gt;Can not use a custom domain. The app will be hosted on username.pythonanywhere.com&lt;/li&gt;
&lt;li&gt;SSH access available&lt;/li&gt;
&lt;li&gt;Browser-based terminals available&lt;/li&gt;
&lt;li&gt;The application setup is extremely easy.&lt;/li&gt;
&lt;li&gt;Root level access not available&lt;/li&gt;
&lt;li&gt;Apart from web hosting, Only MySQL, Postgres and schedule tasks support is provided&lt;/li&gt;
&lt;li&gt;Good for small to medium traffic apps&lt;/li&gt;
&lt;li&gt;The developer needs to take care of code&lt;/li&gt;
&lt;li&gt;Pricing &lt;a href="https://www.pythonanywhere.com/pricing/"&gt;https://www.pythonanywhere.com/pricing/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pythonanywhere.com/?affiliate_id=000f9990"&gt;Create a free account&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pythoncircle.com/post/18/how-to-host-django-app-on-pythonanywhere-for-free/"&gt;Hosting the Django app on PythonAnyWhere&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to add points, please comment below.&lt;/p&gt;

&lt;p&gt;Originally published on &lt;a href="https://www.pythoncircle.com/post/698/aws-ec2-vs-pythonanywhere-for-hosting-django-application/"&gt;PythonCircle.Com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>5 lesser used Django template tags</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Tue, 08 Oct 2019 16:55:42 +0000</pubDate>
      <link>https://dev.to/anuragrana/5-lesser-used-django-template-tags-2ld6</link>
      <guid>https://dev.to/anuragrana/5-lesser-used-django-template-tags-2ld6</guid>
      <description>&lt;p&gt;Originally published at &lt;a href="https://www.pythoncircle.com/post/694/5-lesser-used-django-template-tags/"&gt;&lt;strong&gt;pythoncircle.com&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We already know how to use &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;if-else&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt; and &lt;code&gt;url&lt;/code&gt; template tags in Django. We can also &lt;a href="https://www.pythoncircle.com/post/42/creating-custom-template-tags-in-django/"&gt;create custom template tags&lt;/a&gt; in Django if any requirement is not getting fulfilled with existing tags. &lt;/p&gt;

&lt;p&gt;Here we are introducing you with 5 Django template tags which are lesser-known and used by beginner Django developers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.djangotemplatefiddle.com/f/wV5xSd/"&gt;&lt;strong&gt;1. lorem&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This template tag is used to insert random &lt;code&gt;lorem ipsum&lt;/code&gt; Latin text in the template. This is useful when you want to show the sample data instead of blank space.&lt;/p&gt;

&lt;p&gt;This tag accepts 3 optional parameters: count, method, and random.&lt;/p&gt;

&lt;p&gt;The count is the number of words or paragraphs to produce. The method can have values w for words, p for HTML paragraphs and b for plain-text paragraphs. When the third parameter, random, is used, the text is generated randomly instead of using lorem ipsum text.&lt;/p&gt;

&lt;p&gt;Refer to this &lt;a href="https://www.djangotemplatefiddle.com/f/wV5xSd/"&gt;Django template fiddle&lt;/a&gt; for example and demo. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.djangotemplatefiddle.com/f/SiRLgx/"&gt;&lt;strong&gt;2. templatetag&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Double curly braces are used to &lt;a href="https://www.djangotemplatefiddle.com/f/2GkSFz/"&gt;display variables&lt;/a&gt; in Django templates. What if you want to display just curly braces in template. For this to achieve we can use &lt;code&gt;templatetag&lt;/code&gt; template tag.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.djangotemplatefiddle.com/f/XhzvE3/"&gt;&lt;strong&gt;3. cycle&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This tag accepts the variable number of arguments and outputs the next argument each time this tag is called. Once all arguments have been called, loop restarts from the starting.&lt;/p&gt;

&lt;p&gt;We can use variables or string or a mix of both as arguments for this tag.&lt;/p&gt;

&lt;p&gt;Refer to this &lt;a href="https://www.djangotemplatefiddle.com/f/XhzvE3/"&gt;Django template fiddle&lt;/a&gt; for example and demo. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.djangotemplatefiddle.com/f/qC7C2U/"&gt;&lt;strong&gt;4. firstof&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This template tag accepts the variable number of arguments and returns the first argument which is not False i.e. which is not zero, empty string or &lt;code&gt;False&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Refer to this &lt;a href="https://www.djangotemplatefiddle.com/f/qC7C2U/"&gt;Django template fiddle&lt;/a&gt; for example and demo. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.djangotemplatefiddle.com/f/vQAgNV/"&gt;&lt;strong&gt;5. phone2numeric&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Converts any phone number to the numeric equivalent even if the phone number is not valid.&lt;/p&gt;

&lt;p&gt;It will not convert any integer or boolean values.&lt;/p&gt;

&lt;p&gt;Refer to this &lt;a href="https://www.djangotemplatefiddle.com/f/vQAgNV/"&gt;Django template fiddle&lt;/a&gt; for example and demo. &lt;/p&gt;

&lt;p&gt;Originally published at &lt;a href="https://www.pythoncircle.com/post/694/5-lesser-used-django-template-tags/"&gt;&lt;strong&gt;pythoncircle.com&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
    </item>
    <item>
      <title>Setting bing image of the day as desktop wallpaper using Python script</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Fri, 23 Aug 2019 11:33:22 +0000</pubDate>
      <link>https://dev.to/anuragrana/setting-bing-image-of-the-day-as-desktop-wallpaper-using-python-script-4o5</link>
      <guid>https://dev.to/anuragrana/setting-bing-image-of-the-day-as-desktop-wallpaper-using-python-script-4o5</guid>
      <description>&lt;p&gt;Originally published at &lt;a href="https://www.pythoncircle.com/post/691/python-script-17-setting-bing-image-of-the-day-as-desktop-wallpaper/?utm_campaign=devto"&gt;&lt;strong&gt;pythoncircle.com&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using this python script, we will try to download the bing image of the day and set it as desktop wallpaper.&lt;/p&gt;

&lt;p&gt;There is a URL which bing itself uses to fetch the image details using AJAX. We will send GET request to this URL to fetch image details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# get image url
&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://www.bing.com/HPImageArchive.aspx?format=js&amp;amp;idx=0&amp;amp;n=1&amp;amp;mkt=en-US"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;image_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Response returned will be in below format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "images": [{
        "startdate": "20190823",
        "fullstartdate": "201908230700",
        "enddate": "20190824",
        "url": "/th?id=OHR.FarmlandLandscape_EN-US6661316442_1920x1080.jpg&amp;amp;rf=LaDigue_1920x1080.jpg&amp;amp;pid=hp",
        "urlbase": "/th?id=OHR.FarmlandLandscape_EN-US6661316442",
        "copyright": "Farmland in Washington state's Palouse region (© Art Wolfe/Getty Images)",
        "copyrightlink": "https://www.bing.com/search?q=palouse+region&amp;amp;form=hpcapt&amp;amp;filters=HpDate%3a%2220190823_0700%22",
        "title": "Harvest time in the Palouse",
        "quiz": "/search?q=Bing+homepage+quiz&amp;amp;filters=WQOskey:%22HPQuiz_20190823_FarmlandLandscape%22&amp;amp;FORM=HPQUIZ",
        "wp": true,
        "hsh": "44238c657b35ce3c11fbf3f59881474e",
        "drk": 1,
        "top": 1,
        "bot": 1,
        "hs": []
    }],
    "tooltips": {
        "loading": "Loading...",
        "previous": "Previous image",
        "next": "Next image",
        "walle": "This image is not available to download as wallpaper.",
        "walls": "Download this image. Use of this image is restricted to wallpaper only."
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Get the image URL from the response JSON and download it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# download and save image
&lt;/span&gt;&lt;span class="n"&gt;img_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_image_url&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'wb'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The image will be saved in the present working directory. Set this image as desktop background using below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="sb"&gt;`&lt;/span&gt;which gsettings&lt;span class="sb"&gt;`&lt;/span&gt; &lt;span class="nb"&gt;set &lt;/span&gt;org.gnome.desktop.background picture-uri file:&lt;span class="nv"&gt;$PWD&lt;/span&gt;/imagename
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command will work for Ubuntu. For other operating systems, please update accordingly.&lt;/p&gt;

&lt;p&gt;The script is available on &lt;a href="https://github.com/anuragrana/Python-Scripts"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can automate the process by scheduling this script to run on system startup.&lt;/p&gt;

&lt;p&gt;Originally published at &lt;a href="https://www.pythoncircle.com/post/691/python-script-17-setting-bing-image-of-the-day-as-desktop-wallpaper/?utm_campaign=devto"&gt;&lt;strong&gt;pythoncircle.com&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Generating Random text in Django Template</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Mon, 19 Aug 2019 07:17:56 +0000</pubDate>
      <link>https://dev.to/anuragrana/generating-random-text-in-django-template-4o30</link>
      <guid>https://dev.to/anuragrana/generating-random-text-in-django-template-4o30</guid>
      <description>&lt;p&gt;&lt;strong&gt;Demo: &lt;a href="https://www.djangotemplatefiddle.com/f/wV5xSd/"&gt;https://www.djangotemplatefiddle.com/f/wV5xSd/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can generate random text in Django templates. This is used to fill the sample data.&lt;/p&gt;

&lt;p&gt;Django provides inbuilt template tag &lt;code&gt;{% lorem %}&lt;/code&gt; for this.&lt;/p&gt;

&lt;p&gt;To generate one paragraph with lorem ipsum text, use the above tag as it &lt;br&gt;
is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="n"&gt;Lorem&lt;/span&gt; &lt;span class="n"&gt;ipsum&lt;/span&gt; &lt;span class="n"&gt;paramgraph&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;lorem&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Lorem ipsum paramgraph --&amp;gt;&lt;/span&gt;
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To generate let's say 2 paragraphs with random text, use tag with parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="n"&gt;paragraph&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;lorem&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- random text paragraph --&amp;gt;&lt;/span&gt;
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Deserunt quisquam dolores minus tempore aperiam itaque minima maxime, atque aperiam libero recusandae quod aliquid sed quo a deserunt, a at rem? Incidunt aut quibusdam est distinctio amet nemo, beatae dolorum fugit corporis recusandae dolorem praesentium vel obcaecati consectetur, voluptas quisquam a neque atque debitis, tenetur eaque nostrum ex?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To generate a few random words, use the tag with below parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;lorem&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- random words of length 10 --&amp;gt;&lt;/span&gt;
autem veritatis quisquam optio quibusdam non qui assumenda dolores alias
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can see (and experiment) the demo here: &lt;a href="https://www.djangotemplatefiddle.com/f/wV5xSd/"&gt;https://www.djangotemplatefiddle.com/f/wV5xSd/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>django</category>
    </item>
    <item>
      <title>For loops in Django</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Wed, 14 Aug 2019 04:42:27 +0000</pubDate>
      <link>https://dev.to/anuragrana/for-loops-in-django-2jdi</link>
      <guid>https://dev.to/anuragrana/for-loops-in-django-2jdi</guid>
      <description>&lt;p&gt;Originally published at &lt;a href="https://www.pythoncircle.com/post/685/for-loop-in-django-template/"&gt;pythoncircle.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Demo of the code used below: &lt;a href="https://www.djangotemplatefiddle.com/f/CftUNu/"&gt;DjangoTemplateFiddle.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For loop is used to iterate over any iterable object, accessing one item at a time and making it available inside the for loop body.&lt;/p&gt;

&lt;p&gt;For example, if you want to create a drop down of countries in Django template, you can use the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;country_list&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"{{country}}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endfor&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To iterate over a dictionary of people's name and their age, just like you would do in Python, use below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}},&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;br&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endfor&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Checking if iterable used in for loop is empty:
&lt;/h3&gt;

&lt;p&gt;Let's say you want to display new messages to logged-in user. You fetched all the new messages from the database and stored them in a list and passed to render function along with the template. &lt;/p&gt;

&lt;p&gt;Now you can either check if the message list is empty or not and then display the message accordingly. Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;br&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endfor&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;No&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endif&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or you can use &lt;code&gt;{% empty %}&lt;/code&gt; tag along with &lt;code&gt;{% for %}&lt;/code&gt; tag as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;No&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endfor&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Break in Django for loop:
&lt;/h3&gt;

&lt;p&gt;That might be a piece of bad news for you. There is no break statement in Django template For loop.&lt;/p&gt;

&lt;p&gt;Depending on your requirement you can do one of the following.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1&lt;/strong&gt; - Iterate over the whole list but do not perform any action if the condition is not matched.&lt;/p&gt;

&lt;p&gt;For example, you are printing numbers from a list and you need to exit the list as soon as number 99 is encountered. Normally this would be done as below in Python.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="mi"&gt;99&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But there is no break statement in Django template For loop. You can achieve the same functionality (almost) as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="n"&gt;isBreak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="mi"&gt;99&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="n"&gt;isBreak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endif&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;isBreak&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;# this is a comment. Do nothing. #}
&lt;/span&gt;    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endif&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endfor&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Option 2&lt;/strong&gt; - You can create your &lt;a href="https://www.pythoncircle.com/post/42/creating-custom-template-tags-in-django/"&gt;own custom template tag&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;You can experiment with Django template or can create your own fiddle and share with others. &lt;a href="https://www.djangotemplatefiddle.com/"&gt;Django Template Fiddle&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
    </item>
    <item>
      <title>try .. except .. else .. in python with example</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Thu, 25 Jul 2019 16:07:03 +0000</pubDate>
      <link>https://dev.to/anuragrana/try-except-else-in-python-with-example-ih0</link>
      <guid>https://dev.to/anuragrana/try-except-else-in-python-with-example-ih0</guid>
      <description>&lt;p&gt;This post was originally published on &lt;a href="https://www.pythoncircle.com/post/664/try-except-else-in-python-with-example/"&gt;&lt;strong&gt;PythonCircle.com&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will face at least two types of errors while coding with python. Syntax errors and exceptions.&lt;/p&gt;

&lt;p&gt;Syntax errors are also known as parsing errors. Compiler informs us about the parsing errors using an arrow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;rana&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;brahma&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;python3&lt;/span&gt;
&lt;span class="n"&gt;Python&lt;/span&gt; &lt;span class="mf"&gt;3.5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Nov&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="mi"&gt;2018&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;43&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;GCC&lt;/span&gt; &lt;span class="mf"&gt;5.4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;20160609&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;linux&lt;/span&gt;
&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="s"&gt;"help"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"copyright"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"credits"&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s"&gt;"license"&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;more&lt;/span&gt; &lt;span class="n"&gt;information&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                   &lt;span class="o"&gt;^&lt;/span&gt;
&lt;span class="nb"&gt;SyntaxError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;invalid&lt;/span&gt; &lt;span class="n"&gt;syntax&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even when your code is perfect syntax wise, some lines may report an error when you try to execute them. Errors reported at runtime are known as exceptions.&lt;/p&gt;

&lt;p&gt;For an uninterrupted execution, we must handle the exceptions properly. We can do so by using &lt;code&gt;try except&lt;/code&gt; block. &lt;/p&gt;

&lt;p&gt;In below example, we are trying to add a string and an integer. When the statement is executed it throws an exception.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'rana'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Can&lt;/span&gt;&lt;span class="s"&gt;'t convert '&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="s"&gt;' object to str implicitly
&amp;gt;&amp;gt;&amp;gt; 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we don't handle this exception, the program will exit abruptly. Handle the exception using &lt;code&gt;try except&lt;/code&gt; statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'rana'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Some useful message to debug the code'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; 
&lt;span class="n"&gt;Some&lt;/span&gt; &lt;span class="n"&gt;useful&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;debug&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the above piece of code is executed, an exception is thrown but is then caught by &lt;code&gt;except&lt;/code&gt; block which in turn print a useful message which helps in debugging the code.&lt;/p&gt;

&lt;p&gt;try ... except statement have an optional &lt;code&gt;else&lt;/code&gt; clause. Else part is executed when there is no exception thrown in &lt;code&gt;try&lt;/code&gt; clause. Else part is executed before &lt;code&gt;finally&lt;/code&gt; clause.&lt;/p&gt;

&lt;p&gt;Let's say you are trying to open a file in try block (just an example) and it is possible there might occur some error in opening the file, we will handle exception in except block. If there is no exception thrown, the file is opened successfully, we have to close the file descriptor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'filename.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Some exception in opening the file'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's better to add code to &lt;code&gt;else&lt;/code&gt; clause instead of adding code to &lt;code&gt;try&lt;/code&gt; clause. This helps in avoiding catching the exception which was not raised by code being protected in &lt;code&gt;try&lt;/code&gt; caluse. For example in addition to opening file in &lt;code&gt;try&lt;/code&gt; clause, we were also trying to convert a variable to an integer. Imaging file opened just fine but conversion to int threw an exception which will be reported by except block as an exception in opening file, which will be misleading. See the example code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'filename.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'five'&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'can not open file'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; 
&lt;span class="n"&gt;can&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above could be rewritten as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'filename.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'file was opened'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'can not open file'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'going to close file'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'five'&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'exception in number conversion'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;file was opened
going to close file
exception in number conversion
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes sure that actual exception is reported by except clause.&lt;/p&gt;

&lt;p&gt;Now the question arises if we should use &lt;code&gt;except&lt;/code&gt; or &lt;code&gt;else&lt;/code&gt; block to control the execution flow?&lt;/p&gt;

&lt;p&gt;In python using exception to control the execution flow is normal. For example, iterators use &lt;code&gt;stopIteration&lt;/code&gt; exception to signal the end of items.&lt;/p&gt;

&lt;p&gt;You can read more about exceptions by visiting the links in the reference section below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/16138232/is-it-a-good-practice-to-use-try-except-else-in-python"&gt;https://stackoverflow.com/questions/16138232/is-it-a-good-practice-to-use-try-except-else-in-python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/tutorial/errors.html"&gt;https://docs.python.org/3/tutorial/errors.html&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Image source: quotemaster.org &lt;/p&gt;

&lt;p&gt;This post was originally published on &lt;a href="https://www.pythoncircle.com/post/664/try-except-else-in-python-with-example/"&gt;&lt;strong&gt;PythonCircle.com&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;More from Author:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.pythoncircle.com/post/688/preventing-cross-site-scripting-attack-on-your-django-website/"&gt;Preventing Cross-Site Scripting Attack On Your Django Website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pythoncircle.com/post/687/how-to-generate-atomrss-feed-for-django-website/"&gt;How To Generate Atom/Rss Feed For Django Website&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Top 5 Python Books</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Wed, 24 Jul 2019 16:37:12 +0000</pubDate>
      <link>https://dev.to/anuragrana/top-5-python-books-2j2i</link>
      <guid>https://dev.to/anuragrana/top-5-python-books-2j2i</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W8xn---F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A8hTsEi56dAOkrV-qiwR7ig.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W8xn---F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A8hTsEi56dAOkrV-qiwR7ig.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post was originally published on &lt;a href="https://www.pythoncircle.com/post/646/top-5-python-books/"&gt;&lt;strong&gt;pythoncircle.com&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For students and newbie python programmers asking questions like ‘what is the best book for python?’, here is the list of top 5 python books.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://amzn.to/2AgVKp0"&gt;&lt;strong&gt;Automate the Boring Stuff with Python: Practical Programming for Total Beginners.&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1KxxgVEm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/690/1%2AcSZZ3XO-nfI_Nwz1VjZWyQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1KxxgVEm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/690/1%2AcSZZ3XO-nfI_Nwz1VjZWyQ.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4.7 stars out of 5.&lt;br&gt;&lt;br&gt;
79% programmers rated it 5 star.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Top Reviews:&lt;br&gt;&lt;br&gt;
-&lt;/strong&gt; Excellent Book for Beginners and More  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provides good foundation on python automation
&lt;/li&gt;
&lt;li&gt;I prefer this approach over Learning Python the Hard Way
&lt;/li&gt;
&lt;li&gt;Full of practical examples &amp;amp; worthwhile learning….&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check all the &lt;a href="https://amzn.to/2SocJwu"&gt;reviews here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://amzn.to/2SmR8Vh"&gt;&lt;strong&gt;Python Tricks: A Buffet of Awesome Python Features&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fck7IlKG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AXtaxML0Sxrhxi_Fh71VZWQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fck7IlKG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AXtaxML0Sxrhxi_Fh71VZWQ.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4.8 stars out of 5.&lt;br&gt;&lt;br&gt;
90% programmers rated it 5 star.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Top Reviews:&lt;br&gt;&lt;br&gt;
-&lt;/strong&gt; The quality of this book is definitely not a “trick” — good quality Python tips in a nicely packaged format with extras  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Great resource for intermediate Python users to round out their knowledge of the language
&lt;/li&gt;
&lt;li&gt;A quick read to fill in any gaps in your python knowledge
&lt;/li&gt;
&lt;li&gt;Beginners and Intermediates: excellent Pythonic presentation of good Python programming practice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check all &lt;a href="https://amzn.to/2VayXnn"&gt;reviews here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://amzn.to/2BTaUkj"&gt;Learn Python the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XILAdS54--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/600/1%2AyYtUBdlmNFHgJ1iIC4_6tw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XILAdS54--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/600/1%2AyYtUBdlmNFHgJ1iIC4_6tw.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This book will encourage you to code and see what happens. You will need to search things over the Internet. No spoon feeding.&lt;/p&gt;

&lt;p&gt;3.8 stars out of 5.&lt;br&gt;&lt;br&gt;
51% programmers rated it 5 star.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Top Reviews:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Expect to google most of the material on your own to understand them
&lt;/li&gt;
&lt;li&gt;I love and hate this book
&lt;/li&gt;
&lt;li&gt;Decent book that REQUIRES supplemental internet searches&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check all &lt;a href="https://amzn.to/2LzRdSX"&gt;reviews here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://amzn.to/2LFI8rS"&gt;&lt;strong&gt;Learning Python by Mark Lutz&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hNUVy7ds--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/381/1%2AfC19rkNCPkF6dgZY8Ry0Sg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hNUVy7ds--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/381/1%2AfC19rkNCPkF6dgZY8Ry0Sg.jpeg" alt=""&gt;&lt;/a&gt;&lt;a href="https://amzn.to/2LFI8rS"&gt;&lt;/a&gt;&lt;a href="https://amzn.to/2LFI8rS"&gt;https://amzn.to/2LFI8rS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4 stars out of 5.&lt;br&gt;&lt;br&gt;
50% programmers rated it 5 star.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Top Reviews:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Very dense. Too much apology for being dense. Very detailed, yet inefficient.
&lt;/li&gt;
&lt;li&gt;The Python Bible — not for beginners
&lt;/li&gt;
&lt;li&gt;I found this book to have extremely clear explanations of this language. It’s conceptually well structured, probably because the author has extensive experience teaching this topic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check all &lt;a href="https://amzn.to/2LFI8rS"&gt;reviews here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://amzn.to/2Vcg2Zp"&gt;&lt;strong&gt;Python Programming: An Introduction to Computer Science&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VEEEWgeN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AVcS-xsTMJBXZtkP8fh4X1A.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VEEEWgeN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AVcS-xsTMJBXZtkP8fh4X1A.jpeg" alt=""&gt;&lt;/a&gt;&lt;a href="https://amzn.to/2Vcg2Zp"&gt;&lt;/a&gt;&lt;a href="https://amzn.to/2Vcg2Zp"&gt;https://amzn.to/2Vcg2Zp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4.4 stars out of 5.&lt;br&gt;&lt;br&gt;
69% programmers rated it 5 star.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Top reviews:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wonderful resource for the burgeoning programmer
&lt;/li&gt;
&lt;li&gt;Excellent Introductory College Textbook
&lt;/li&gt;
&lt;li&gt;Love this book because it teaches you the basics, tests your knowledge at the end of the chapter, and then gives you problems that are quite difficult.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check all &lt;a href="https://amzn.to/2Vcg2Zp"&gt;reviews here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>Scraping data of 2019 Indian General Election using Python Request and BeautifulSoup and analyzing…</title>
      <dc:creator>Anurag Rana</dc:creator>
      <pubDate>Wed, 24 Jul 2019 16:35:21 +0000</pubDate>
      <link>https://dev.to/anuragrana/scraping-data-of-2019-indian-general-election-using-python-request-and-beautifulsoup-and-analyzing-1e6f</link>
      <guid>https://dev.to/anuragrana/scraping-data-of-2019-indian-general-election-using-python-request-and-beautifulsoup-and-analyzing-1e6f</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F700%2F1%2AY8ypg1T4mAMOSeHPV856dw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F700%2F1%2AY8ypg1T4mAMOSeHPV856dw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you know that: &lt;/p&gt;

&lt;p&gt;Shankar Lalwani from Indore constituency got 1068569 votes which are highest votes any candidate got in this election season.&lt;/p&gt;

&lt;p&gt;C. R. Patil from Navsari constituency won by highest margin i.e. 689668 votes.&lt;/p&gt;

&lt;p&gt;Bholanath (B.P. Saroj) from Machhlishahr won by just 181 votes which is the lowest margin among all constituencies.&lt;/p&gt;

&lt;p&gt;Total votes cast: 613133300&lt;/p&gt;

&lt;p&gt;NOTA votes cast: 6514558&lt;/p&gt;

&lt;p&gt;You can fetch much more information like above if you know how to fetch data and analyze it.&lt;/p&gt;

&lt;p&gt;Refer below article to know how to Scrape 2019 Indian General election data and analyze it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pythoncircle.com/post/683/scraping-data-of-2019-indian-general-election-using-python-request-and-beautifulsoup-and-analyzing-it/" rel="noopener noreferrer"&gt;&lt;strong&gt;Scraping data of 2019 Indian General Election using Python Request and BeautifulSoup and analyzing it&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beautifulsoup</category>
      <category>python</category>
      <category>webscraping</category>
      <category>requestslibrary</category>
    </item>
  </channel>
</rss>
