<?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: Shantanu Nighot</title>
    <description>The latest articles on DEV Community by Shantanu Nighot (@magbanum).</description>
    <link>https://dev.to/magbanum</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%2F630982%2Fe9f43c87-c287-44ae-a7b4-4d0f769dc122.jpeg</url>
      <title>DEV Community: Shantanu Nighot</title>
      <link>https://dev.to/magbanum</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/magbanum"/>
    <language>en</language>
    <item>
      <title>How to save Environment Variables in Python virtual env</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Sun, 29 Jan 2023 06:45:21 +0000</pubDate>
      <link>https://dev.to/magbanum/how-to-save-environment-variables-in-python-virtual-env-2mm4</link>
      <guid>https://dev.to/magbanum/how-to-save-environment-variables-in-python-virtual-env-2mm4</guid>
      <description>&lt;p&gt;Saving environment variables in a Python virtual environment can be a great way to keep your project's dependencies and settings organized and separate from other tasks on your machine.&lt;/p&gt;

&lt;p&gt;I use Django to create APIs for the front end of my website. Django needs many credentials that should not be exposed to the public or in the GitHub repository. I have already created an article on &lt;a href="https://magbanum.com/blog/how-to-hide-django-secret_key-on-public-repositories"&gt;&lt;strong&gt;How to hide Django SECRET_KEY on Public Repositories.&lt;/strong&gt;&lt;/a&gt; But if we want to use it on a local machine then still have to replace the code with the original credentials.&lt;/p&gt;

&lt;p&gt;Here are the steps to take to save environment variables in a Python virtual environment:&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a virtual environment:
&lt;/h3&gt;

&lt;p&gt;To create a virtual environment, you can use the virtualenv package. To install it, open a terminal and run &lt;code&gt;pip install virtualenv&lt;/code&gt;. Once it is installed, you can create a new virtual environment by running a command &lt;code&gt;virtualenv venv&lt;/code&gt;, where "venv" is the name of your virtual environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Activate the virtual environment:
&lt;/h3&gt;

&lt;p&gt;To activate the virtual environment, run &lt;code&gt;source myenv/bin/activate&lt;/code&gt; on Linux and macOS or &lt;code&gt;source myenv\Scripts\activate&lt;/code&gt; on Windows. Once the virtual environment is active, the name of the virtual environment will appear in the command prompt.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zwkojNbX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674969239910/77a4306e-9081-4a39-a74b-c17165aaff64.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zwkojNbX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674969239910/77a4306e-9081-4a39-a74b-c17165aaff64.png" alt="" width="516" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Set environment variables:
&lt;/h3&gt;

&lt;p&gt;To set an environment variable, you can use the export command on Linux and macOS or the set command on Windows. For example, to set an environment variable named &lt;code&gt;DJANGO_SETTINGS_MODULE&lt;/code&gt; with the value &lt;code&gt;myproject.settings&lt;/code&gt;, you can run &lt;code&gt;export DJANGO_SETTINGS_MODULE=myproject.settings&lt;/code&gt; on Linux and macOS or &lt;code&gt;set DJANGO_SETTINGS_MODULE=myproject.settings&lt;/code&gt; on Windows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VpoO55ES--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674969173395/9e6b444c-93ea-449f-902e-dac0bb42bfc7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VpoO55ES--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674969173395/9e6b444c-93ea-449f-902e-dac0bb42bfc7.png" alt="" width="496" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep in mind that the environment variables set through the command prompt are temporarily saved in the environment and will be destroyed once the environment is deactivated. Use the following method to set the variables automatically every time the environment is activated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Save environment variables:
&lt;/h3&gt;

&lt;p&gt;To save environment variables so they persist even after you close the terminal or deactivate the virtual environment, you need to add the &lt;code&gt;export&lt;/code&gt; or &lt;code&gt;set&lt;/code&gt; commands to a shell script.&lt;/p&gt;

&lt;p&gt;On Linux and macOS, create a file named activate in the bin directory of your virtual environment with the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/sh
export SECRET_KEY="django-insecure- *************"

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

&lt;/div&gt;



&lt;p&gt;On Windows, find the file named activate or create a file named &lt;code&gt;activate&lt;/code&gt; in the &lt;code&gt;Scripts&lt;/code&gt; directory of your virtual environment with the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set SECRET_KEY="django-insecure- *************"

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JzaZ9Zw5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674969514960/74427318-3eb3-4463-b2cd-3dff18b41d10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JzaZ9Zw5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674969514960/74427318-3eb3-4463-b2cd-3dff18b41d10.png" alt="" width="632" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Use saved environment variables:
&lt;/h3&gt;

&lt;p&gt;We can use the saved variables using &lt;code&gt;os.environ.get()&lt;/code&gt;. In my case, I want to use the saved secret key for my Django project. In &lt;code&gt;settings.py&lt;/code&gt; file, I will import the OS module using &lt;code&gt;import os&lt;/code&gt;. Then I will get the saved environment variable &lt;code&gt;SECRET_KEY&lt;/code&gt; using the command &lt;code&gt;os.environ.get('SECRET_KEY')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GzN4A8yv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674970193681/fe8edf6a-5c04-4cbb-abd4-010047476e48.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GzN4A8yv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674970193681/fe8edf6a-5c04-4cbb-abd4-010047476e48.png" alt="" width="626" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Make sure you do not expose &lt;code&gt;activate&lt;/code&gt; file in your GitHub repository. To ensure this add your env directory to &lt;code&gt;.gitignore&lt;/code&gt; file. Directories and files included in &lt;code&gt;.gitignore&lt;/code&gt; are not included while pushing the code to the repository.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CeHvWK0j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674970408251/41b9cdae-6304-4136-9880-d9fe82025b33.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CeHvWK0j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1674970408251/41b9cdae-6304-4136-9880-d9fe82025b33.png" alt="" width="425" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Deactivate the virtual environment:
&lt;/h3&gt;

&lt;p&gt;To deactivate the virtual environment, you can run the &lt;code&gt;deactivate&lt;/code&gt; command. This will take you out of the virtual environment and return to the global Python installation on your machine.&lt;/p&gt;

&lt;p&gt;By following these steps, you can successfully save environment variables in a Python virtual environment, keeping your project dependencies and settings organized and separate from other projects on your machine.&lt;/p&gt;

&lt;p&gt;Please let me know if I am missing something in this tutorial or need help with any step.&lt;/p&gt;

&lt;p&gt;Thanks for reading. See you in the next article.&lt;/p&gt;

</description>
      <category>python</category>
      <category>environment</category>
      <category>variables</category>
      <category>django</category>
    </item>
    <item>
      <title>A quick and easy alternative to Heroku is Railway.app</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Sun, 16 Oct 2022 10:46:30 +0000</pubDate>
      <link>https://dev.to/magbanum/a-quick-and-easy-alternative-to-heroku-is-railwayapp-32f5</link>
      <guid>https://dev.to/magbanum/a-quick-and-easy-alternative-to-heroku-is-railwayapp-32f5</guid>
      <description>&lt;p&gt;Suppose you are currently using Heroku to deploy your projects like Django apps. In that case, you should have received mail from Heroku that they are going to stop free dynos, free databases and all other free services currently provided. This was very sad, as Heroku became my favourite cloud platform last year. I chose Heroku over other cloud platforms because it provides services for free as well as the deployment process is very straightforward. You can deploy your project in very less configurations.&lt;/p&gt;

&lt;p&gt;But now as Heroku is going to stop its free services, I had to search for another platform to deploy my existing as well as upcoming projects. At first, I explored famous platforms like Google Cloud Platform, Amazon Web Services and Microsoft Azure. But all of them provided free deployment for a limited time or limited credits.&lt;/p&gt;

&lt;p&gt;After searching the Web, I found the &lt;a href="https://railway.app?referralCode=bnH7Bi"&gt;Railway.app&lt;/a&gt; website and decided to give it a try after going through its features and free plan. A few features that I like personally are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quick and easy setup&lt;/li&gt;
&lt;li&gt;Deploy from GitHub Repository&lt;/li&gt;
&lt;li&gt;Chose Database of your choice&lt;/li&gt;
&lt;li&gt;Automatic Deployments&lt;/li&gt;
&lt;li&gt;Apps do not sleep like Heroku apps&lt;/li&gt;
&lt;li&gt;Templates for production-ready applications&lt;/li&gt;
&lt;li&gt;Configurations same as Heroku&lt;/li&gt;
&lt;li&gt;Full access to Database unlike Heroku&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The starter plan of the Railway.app includes $5.00 of usage each month with an execution time limit of 500 hours which I think is sufficient for small projects on your portfolio.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--huIiT2v4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665898898188/nIwWoAx-S.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--huIiT2v4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665898898188/nIwWoAx-S.png" alt="image.png" width="880" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, I will show you how to move your Django project from Heroku to Railway efficiently. My &lt;a href="https://octoprofile.herokuapp.com/"&gt;Octoprofile&lt;/a&gt; project is currently deployed on Heroku and I will move it to Railway.app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create new project and add Config variables
&lt;/h2&gt;

&lt;p&gt;Sign up on Railway.app and go to your dashboard to create a new project. I will be deploying the project from the same GitHub repository used to deploy on Heroku. We are able to do this because configuration files like Proc file and runtime file required are the same for both platforms.&lt;/p&gt;

&lt;p&gt;After connecting your GitHub account and selecting the respective repository, &lt;strong&gt;DO NOT&lt;/strong&gt; Deploy directly because we are missing the Config variables like SECRET KEY and DATABASE URL which will fail the deployment. If your Project does not have any variables or Database connected then only you can click on &lt;strong&gt;Deploy Now&lt;/strong&gt;. Click on &lt;strong&gt;Add variables&lt;/strong&gt; to add variables from the Heroku dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AF1_SFxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665901106598/11SXxrtzj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AF1_SFxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665901106598/11SXxrtzj.png" alt="image.png" width="880" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get the variables for your project go to the Heroku dashboard and you can find &lt;strong&gt;Config Vars&lt;/strong&gt; in the settings of your app. Copy and paste all Key-value pairs to Your Railway project except for the DATABASE_URL. Because we are not going to use the Heroku Database. We will create a new Database for the Railway.app project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WYaOMrme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665901480883/JvyXNQ24Y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WYaOMrme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665901480883/JvyXNQ24Y.png" alt="image.png" width="880" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a new Database and link it to your Project
&lt;/h2&gt;

&lt;p&gt;To create a new database, click on the &lt;strong&gt;New&lt;/strong&gt; button inside your project dashboard and click on Database. You can select any Database like PostgresSQL, Redis, MongoDB and MySQL. I will use PostgresSQL for this project.&lt;/p&gt;

&lt;p&gt;Once it's created, go to connect tab and copy &lt;strong&gt;Postgres Connection URL&lt;/strong&gt;. Now go to your project and create a new variable DATABASE_URL and paste copied link to its value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that whenever you make any changes in the project like adding new variables, every time the Railway will auto-deploy the changes. And will fail many times until all configurations are in place and this is fine. You can always check the logs to know what went wrong.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, you should see something like this. The deployment is successful and you will get a link to your website. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yGnWG8jV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665912395245/MQxFu8Fle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yGnWG8jV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665912395245/MQxFu8Fle.png" alt="image.png" width="829" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to do after the successful deployment.
&lt;/h2&gt;

&lt;p&gt;First thing is to change the default domain name provided by Railway and set it to something that makes sense. I will set it to &lt;a href="https://octoprofile.up.railway.app/"&gt;octoprofile.up.railway.app&lt;/a&gt;. You can also set up a custom domain if you have one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iFbhtABb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665912658573/57JzksR4o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iFbhtABb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665912658573/57JzksR4o.png" alt="image.png" width="831" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if you visit the URL, you may get &lt;code&gt;Bad Request (400)&lt;/code&gt;. This is because you might have set &lt;strong&gt;ALLOWED_HOST&lt;/strong&gt; as &lt;code&gt;***.herokuapp.com&lt;/code&gt; in the** settings.py** file of the Django project. You will change it to the one provided by Railway.app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cHmI5Fhk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665913022846/eNq2aMN0L.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cHmI5Fhk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665913022846/eNq2aMN0L.png" alt="image.png" width="382" height="49"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you push changes to GitHub, Railway.app will deploy the changes and the site will appear on the link provided.&lt;/p&gt;

&lt;p&gt;You can see my deployed site at &lt;a href="https://octoprofile.up.railway.app/"&gt;octoprofile.up.railway.app&lt;/a&gt;. Enter your GitHub username and hit enter.&lt;/p&gt;

&lt;p&gt;If you want to create a user just go to Postgres Database and add a new row in the &lt;strong&gt;auth_user&lt;/strong&gt; table. Don't forget to give &lt;code&gt;True&lt;/code&gt; for the &lt;strong&gt;is_superuser&lt;/strong&gt; field else you will not able to login to the Admin page. Similarly, you can add other data into respective tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DNCN9NvA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665915842509/N62sI7ryw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DNCN9NvA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665915842509/N62sI7ryw.png" alt="image.png" width="813" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is all to move from Heroku to Railway.app. Let me know if you want to add anything or if any steps are missed. Also, you can comment here if you get any errors while deploying so we can solve it together.&lt;/p&gt;

&lt;p&gt;Thanks for reading. See you in the next article. 😊&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be better than yesterday's you.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>An alternative method to view your Heroku Database tables</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Sun, 09 Oct 2022 08:46:04 +0000</pubDate>
      <link>https://dev.to/magbanum/an-alternative-method-to-view-your-heroku-database-tables-4e90</link>
      <guid>https://dev.to/magbanum/an-alternative-method-to-view-your-heroku-database-tables-4e90</guid>
      <description>&lt;p&gt;In this article, I will tell you about an alternative method to view your database from the Heroku platform. Check the tables of your Heroku Postgres Database used by your website or an application.&lt;/p&gt;

&lt;p&gt;When I deployed my application on Heroku, I was curious about checking tables created by Django and Models. But was unable to check it as Heroku provides no specific functionality to view what's inside a database. After exploring, I found this application that helped me go through my database tables.&lt;/p&gt;

&lt;p&gt;Following are the quick steps to get started,&lt;/p&gt;

&lt;h2&gt;
  
  
  Download a small application
&lt;/h2&gt;

&lt;p&gt;Firstly download the application file respective to your machine from &lt;a href="https://github.com/sosedoff/pgweb/releases/tag/v0.11.12"&gt;this link&lt;/a&gt; and extract the zip file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the application
&lt;/h2&gt;

&lt;p&gt;Run this application and this will start one localhost session at &lt;code&gt;https://localhost:8081&lt;/code&gt; or any other port. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7pig-Krs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665299072771/0pQoLCRboo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7pig-Krs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665299072771/0pQoLCRboo.png" alt="image.png" width="880" height="97"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Visit localhost
&lt;/h2&gt;

&lt;p&gt;After visiting the respective localhost link, you will see the following page. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--loGsgAi4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665299440355/cw3roJc3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--loGsgAi4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665299440355/cw3roJc3i.png" alt="image.png" width="880" height="522"&gt;&lt;/a&gt;You will get an option to connect with your database using a standard URI, Database credentials or SSH.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Database credentials
&lt;/h2&gt;

&lt;p&gt;Visit your application dashboard on Heroku, go to the Resources tab and click on Heroku Postgres. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8UIoEKFj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665299843338/wio-gVlF2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8UIoEKFj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665299843338/wio-gVlF2.png" alt="image.png" width="880" height="450"&gt;&lt;/a&gt;On the Datastores page, go to the Settings tab and click on View Credentials. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WnpVQJU4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665300006493/XI-fW0Tlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WnpVQJU4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665300006493/XI-fW0Tlc.png" alt="image.png" width="880" height="277"&gt;&lt;/a&gt;You will get the Hostname, Database, User, Port, Password, URI and Heroku CLI information. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QytD-_dP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665300312234/v3heOvwTB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QytD-_dP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665300312234/v3heOvwTB.png" alt="image.png" width="880" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect to Database
&lt;/h2&gt;

&lt;p&gt;You can use these credentials to connect to your database. For this article, I will be using the URI method. Copy the URI from the Heroku dashboard, paste it into the &lt;strong&gt;Scheme&lt;/strong&gt; tab and click on connect button. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9mbWWvpv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665300653393/HOVZgq9hX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9mbWWvpv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665300653393/HOVZgq9hX.png" alt="image.png" width="880" height="622"&gt;&lt;/a&gt;This will open your database and the tables in it. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QxzEFU-l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665300861531/I39TN74_D.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QxzEFU-l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1665300861531/I39TN74_D.png" alt="image.png" width="880" height="452"&gt;&lt;/a&gt;You can get a lot of information about your databases such as tables, rows and table structure. Find out now yourself by following the steps above.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article. You can also subscribe to my newsletter to receive my articles straight to your inbox.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be better than Yesterday's you!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>heroku</category>
      <category>postres</category>
      <category>alternatives</category>
      <category>developer</category>
    </item>
    <item>
      <title>Let Cloudinary handle image uploads in your Django application.</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Sun, 15 May 2022 12:17:48 +0000</pubDate>
      <link>https://dev.to/magbanum/let-cloudinary-handle-image-uploads-in-your-django-application-eck</link>
      <guid>https://dev.to/magbanum/let-cloudinary-handle-image-uploads-in-your-django-application-eck</guid>
      <description>&lt;p&gt;Hi, everyone,&lt;/p&gt;

&lt;p&gt;It's been a while since I uploaded my last article. I would love to tell you that I have joined Publicis Sapient six months ago as a Junior Associate Technology. And now, I have been promoted to Associate Technology L1. These first six months were so tough for me as I was new to this corporate world. I got exposed to technologies like Java, JavaScript, MongoDB, SQL and MySQL. Everything was so overwhelming, and I was unable to give time for blogging. But after spending six months working hard for the company, I decided to spare some time for my blog.&lt;/p&gt;

&lt;p&gt;In this article, we are going to create a simple Django application called PhotoGallery where image uploads are handled by Cloudinary. The main focus of this article is to know how to integrate Cloudinary with Django models.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cloudinary
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The company provides cloud-based image and video management services. It enables users to upload, store, manage, manipulate, and deliver images and videos for websites and apps. &lt;a href="https://en.wikipedia.org/wiki/Cloudinary"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Overall, I have been using Cloudinary for a long time and would recommend it to developers who not only want to store images and videos but wants to optimise them for websites and apps.&lt;/p&gt;

&lt;p&gt;We are going to create a Django application that displays the Images from the database and has a form to upload Images with its Alt name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pre-requisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Django &lt;code&gt;pip install Django&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Cloudinary &lt;code&gt;pip install cloudinary&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create an account on &lt;a href="https://cloudinary.com/invites/lpov9zyyucivvxsnalc5/hhqbagkwhsnz7ca6jssy?t=default"&gt;Cloudinary.com&lt;/a&gt; and note down the cloud name, API key and API secret key. We will need these to integrate the Cloudinary account with our application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PlIQZdBA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652592860623/86I1gY1oa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PlIQZdBA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652592860623/86I1gY1oa.png" alt="image.png" width="880" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a Django project
&lt;/h3&gt;

&lt;p&gt;If you are familiar with Django, these parts of the tutorial are piece of cake for you. Before you create a Django project, it is recommended to create a virtual environment to install dependencies for the project within the environment only. Check out the article &lt;a href="https://dev.to/magbanum/a-virtual-environment-in-powershell-2mnm"&gt;A virtual environment in Powershell&lt;/a&gt; to know how to create virtual environments. Make sure to install Django using &lt;code&gt;pip install Django&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Run the following command to create a Django project. I am using &lt;strong&gt;photogallery&lt;/strong&gt; but you are free to use any name you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; django-admin startproject photogallery

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create an app named &lt;strong&gt;gallery&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now that the project is created open the directory with the project name using &lt;code&gt;cd photogallery&lt;/code&gt;. And create an app with the name &lt;strong&gt;gallery&lt;/strong&gt; using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; python manage.py startapp gallery

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

&lt;/div&gt;



&lt;p&gt;The project structure should look like this, &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ElOVMJqR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652538535815/7j0xU7eES.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ElOVMJqR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652538535815/7j0xU7eES.png" alt="Django project structure" width="276" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add a new app to the INSTALLED_APP list in the settings.py file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./photogallery/settings.py

INSTALLED_APPS = [
    .
    .
    'gallery',
]

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

&lt;/div&gt;



&lt;p&gt;Also, add cloudinary credentials we copied earlier from the website in the settings file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./photogallery/settings.py

CLOUDINARY = {
  'cloud_name': 'your cloud name',  
  'api_key': 'your cloudinary API key',  
  'api_secret': 'your cloudinary API secret key',  
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a Model to store photos
&lt;/h3&gt;

&lt;p&gt;We will create a Model named &lt;strong&gt;Photo&lt;/strong&gt; with fields like image, alt and upload date. But you are free to add a few extra fields like tags and image descriptions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./gallery/models.py

from django.db import models
from cloudinary.models import CloudinaryField

class Photo(models.Model):
    image = CloudinaryField('image')
    alt = models.CharField(max_length=200)
    uploaded = models.DateTimeField(auto_now_add=True)

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

&lt;/div&gt;



&lt;p&gt;You should have noticed that we added &lt;strong&gt;CloudinaryField&lt;/strong&gt; instead of Django's ImageField. It's a special Field provided by Cloudinary for uploading images and videos. The first argument is the type of file expected for upload. There is a lot more you can do with this field. Check out &lt;a href="https://cloudinary.com/documentation/django_image_and_video_upload"&gt;this&lt;/a&gt; documentation to know more about the cloudinary fields and arguments.&lt;/p&gt;

&lt;p&gt;Register your model &lt;code&gt;Photo&lt;/code&gt; by adding the following code in &lt;code&gt;admin.py&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./gallery/admin.py

from gallery.models import Photo

admin.site.register(Photo)

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

&lt;/div&gt;



&lt;p&gt;Don't forget to run the following commands, these will create migrations and new tables in the database from our models.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; python manage.py makemigrations
&amp;gt;&amp;gt; python manage.py migrate

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

&lt;/div&gt;



&lt;p&gt;To quickly check how our model is working, you can create a superuser to access the admin panel. Run the following command to create a superuser. It will ask you for your username, email, and password.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; python manage.py createsuperuser

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

&lt;/div&gt;



&lt;p&gt;After creating a superuser, you can run command &lt;code&gt;python manage.py runserver&lt;/code&gt;, access the admin panel at &lt;code&gt;/admin&lt;/code&gt; and log in using these credentials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qCrEJ9Ss--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652610699865/Q0_MshXcS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qCrEJ9Ss--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652610699865/Q0_MshXcS.png" alt="image.png" width="880" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try to upload a few images and with successful submission of the form, you should see something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jr7BaDEF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652611233751/yfy3iM32j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jr7BaDEF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652611233751/yfy3iM32j.png" alt="image.png" width="880" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that the image is saved in link form and not the image directly. I will show you how to access this image in the template later in the tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a form to upload photos
&lt;/h3&gt;

&lt;p&gt;Let's create a simple form to upload images directly from the homepage. forms.py is not created by default so you will need to create it in the app directory. You can do a lot more with the Django forms than we did in this tutorial. Check out &lt;a href="https://docs.djangoproject.com/en/4.0/ref/forms/"&gt;Django documentation&lt;/a&gt; to know more about Django forms.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./gallery/forms.py

from django.forms import ModelForm      
from .models import Photo

class PhotoForm(ModelForm):
  class Meta:
      model = Photo
      fields = ['image', 'alt']

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a class-based view
&lt;/h3&gt;

&lt;p&gt;We are creating a class-based view that inherits Django's generic view called CreateView. We are using CreateView instead of ListView because we are passing objects as well as a form in the same view and using ListView does not allow us to render the form correctly. We have passed the Photo model, PhotoForm and a home.html template to the PhotoList view. Don't worry, we are going to create this simple template later in this tutorial.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./gallery/views.py

from django.urls import reverse
from gallery.models import Photo
from django.views.generic import CreateView
from gallery.forms import PhotoForm

class PhotoList(CreateView):
    model = Photo
    form_class = PhotoForm
    template_name = "gallery/home.html"

    def get_context_data(self, **kwargs):
        context = super(PhotoList, self).get_context_data(**kwargs)
        context['photos'] = Photo.objects.all().order_by('-uploaded')
        return context

    def get_success_url(self):
        return reverse('home')

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

&lt;/div&gt;



&lt;p&gt;We are passing extra objects created from our Photo model by calling the method &lt;code&gt;get_context_data&lt;/code&gt;. Later you can access these objects in a template using the context name &lt;code&gt;photos&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add URL to homepage
&lt;/h3&gt;

&lt;p&gt;In &lt;code&gt;photogallery/urls.py&lt;/code&gt; add the following lines of code to connect our PhotoList view to the URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./photogallery/urls.py

from gallery.views import PhotoList

urlpatterns = [
    .
    path('', PhotoList.as_view(), name='home')
]

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a template for the homepage
&lt;/h3&gt;

&lt;p&gt;This is the last step of our tutorial. We need a template to show our photos and the PhotoForm to upload photos with alt names. We have already defined the template name in View as &lt;code&gt;home.html&lt;/code&gt; so we will create the same in a new directory &lt;code&gt;gallery/templates/gallery/home.html&lt;/code&gt; and add the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./gallery/templates/gallery/home.html

&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Photo Gallery&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        .grid-container {
          display: grid;
          grid-template-columns: auto auto auto auto;
          gap: 10px;
          background-color: #dadada;
          padding: 10px;
        }
    &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1 style="text-align:center;"&amp;gt;Photo gallery&amp;lt;/h1&amp;gt;
    &amp;lt;div class="grid-container"&amp;gt;
        {% for photo in photos %}
            &amp;lt;img src={{ photo.image.url }} alt="{{ photo.alt }}" style="width: 100%;"&amp;gt;
        {% endfor %}
    &amp;lt;/div&amp;gt;
    &amp;lt;br&amp;gt;
    &amp;lt;center&amp;gt;
        &amp;lt;h3&amp;gt;Upload your photos here.&amp;lt;/h3&amp;gt;
        &amp;lt;form method="POST" action="" enctype="multipart/form-data"&amp;gt;
            {% csrf_token %}
            {{ form }}
            &amp;lt;input type="submit" value="Submit"&amp;gt;
        &amp;lt;/form&amp;gt;
    &amp;lt;/center&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;When we created the PhotoList view, we passed an extra context &lt;code&gt;photos&lt;/code&gt; which stores all Photo objects. That same context is referred in for loop to iterate through each object. Then each object has an image property which further has a URL property which points to the cloudinary cloud location.&lt;/p&gt;

&lt;p&gt;After the photo gallery, we have a simple form to upload photos. And the final application looks like the following,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BP52NsVh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652613798537/RpQs4C9Hsx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BP52NsVh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1652613798537/RpQs4C9Hsx.png" alt="image.png" width="880" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it, but you can not and should not stop here especially if you are a beginner. Dig deeper, check out the Django documentation and add extra features and functionalities to your application. For example, you can add authentication required for forms to restrict others from uploading photos to your gallery. Check out the GitHub repository for the source code of this project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/magbanum/photogallery/"&gt;https://github.com/magbanum/photogallery/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I said earlier, the main focus of this article was to know how cloudinary can help us manage image uploads efficiently, hassle-free and without much integration process. All we needed is credentials and a cloudinaryField. And the bonus is that Cloudinary not only delivers the images but at the same time optimizes them. Which helps your application load faster.&lt;/p&gt;

&lt;p&gt;I hope you found this article helpful. Please let me know if I did something wrong in this article or have any doubt regarding any step. I will be happy to help you with my knowledge and understanding.&lt;/p&gt;

&lt;p&gt;Thanks for reading.🌻&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be better than yesterday's you!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cloudinary</category>
      <category>django</category>
      <category>python</category>
      <category>developer</category>
    </item>
    <item>
      <title>All about python dictionary.</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Sat, 09 Oct 2021 12:21:44 +0000</pubDate>
      <link>https://dev.to/magbanum/all-about-python-dictionary-4llm</link>
      <guid>https://dev.to/magbanum/all-about-python-dictionary-4llm</guid>
      <description>&lt;p&gt;Python dictionary is one of the most used and powerful data structures in python. It is a collection of data in the form of key-value pairs. It is a feature-rich data structure used widely when working with structured data. Python dictionaries have many built-in methods that are used to access, update and delete the data from them which makes them easy to use. Python dictionaries and lists have similar properties but differ in how the data is accessed. Python lists use zero-based indexing whereas dictionaries use keys for referencing the data associated with it. Let's learn more about dictionaries and how to work efficiently with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The article explains the basic syntax and characteristics of python dictionaries.&lt;/li&gt;
&lt;li&gt;The article includes basic CRUD operations with dictionaries.&lt;/li&gt;
&lt;li&gt;The article has provided information about the built-in dictionary methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
What is a Python dictionary? &lt;/li&gt;
&lt;li&gt;
Properties of dictionary &lt;/li&gt;
&lt;li&gt;
Keys &lt;/li&gt;
&lt;li&gt;
Values &lt;/li&gt;
&lt;li&gt;
CRUD operations with a dictionary &lt;/li&gt;
&lt;li&gt;Built-in dictionary methods&lt;/li&gt;
&lt;li&gt;
Summary &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is a Python dictionary?
&lt;/h3&gt;

&lt;p&gt;The python Dictionary is a collection of data in the form of key-value pairs. Consider python Dictionary a "bag" or a "collection" of values, each with its own and unique label (key).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; my_dict = {"name": "Shantanu Nighot", "age": 21, "grade": "A" , "pass": True }
{"name": "Shantanu Nighot", "age": 21, "grade": "A" , "pass": True }

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

&lt;/div&gt;



&lt;p&gt;In python dictionaries, data is stored as a pair of keys and their associated value separated by a colon (:) and wrapped inside the curly braces ({}). Each key-value pair is separated by a comma (,).&lt;/p&gt;

&lt;h3&gt;
  
  
  Properties of Dictionary:
&lt;/h3&gt;

&lt;p&gt;Some of the important properties of dictionaries are,&lt;/p&gt;

&lt;h4&gt;
  
  
  They are mutable.
&lt;/h4&gt;

&lt;p&gt;Python dictionaries are mutable just like the lists. This means elements of the dictionary can be updated or deleted after the dictionary is defined.&lt;/p&gt;

&lt;h4&gt;
  
  
  They are ordered.
&lt;/h4&gt;

&lt;p&gt;Python dictionaries are ordered. This means that the elements are stored in the order they are added.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do you know? Python dictionaries were unordered till Python version 3.6. After that, they were made ordered from version 3.7. Try running the following code on python version 3.6 and the latest one, and see the difference in results.&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; my_dict = {"name": "Shantanu Nighot", "age": 21, "grade": "A"}
&amp;gt;&amp;gt; print(my_dict)

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

&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  They can be nested.
&lt;/h4&gt;

&lt;p&gt;Python dictionaries just like lists can have another dictionary or list as a value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; bagpack = {"Books": 5, "Camera": 1, "Fruits": {"Apple": 1, "Orange": 2}}
{"Books": 5, "Camera": 1, "Fruits": {"Apple": 1, "Orange": 2}}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Keys:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Keys must be unique.
&lt;/h4&gt;

&lt;p&gt;Python dictionaries do not allow duplicate keys. This means any key is unique throughout the dictionary. If there are duplicate keys defined in the dictionary the last appeared key-value pair is considered only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; mydict = {"name": "Shantanu", "age": 20, "grades": "A", "age": 22}
{'name': 'Shantanu', 'grades': 'A', 'age': 22}

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

&lt;/div&gt;



&lt;p&gt;You can see that the key &lt;code&gt;“age”&lt;/code&gt; appeared multiple times with different values but only the last appearing value (&lt;code&gt;22&lt;/code&gt;) is saved in a dictionary.&lt;/p&gt;

&lt;h4&gt;
  
  
  Keys must be immutable:
&lt;/h4&gt;

&lt;p&gt;Keys in the dictionary can be of any immutable data type. For example string, number, boolean or binary data type can be used as a key in a python dictionary. Tuples being immutable can also be used as a dictionary key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; mydict = {"name": "Shantanu", 21: "age", False: "Palash", (1,2): "numbers"}
{"name": "Shantanu", 21: "age", False: "Palash", (1,2): "numbers"}

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

&lt;/div&gt;



&lt;p&gt;Amazingly, we can also use the python function as a key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; def name(): pass
&amp;gt;&amp;gt;&amp;gt; mydict = {name(): "Shantanu", False: "Palash"}
{name(): "Shantanu", False: "Palash"}
&amp;gt;&amp;gt;&amp;gt; mydict[name()]
Shantanu

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

&lt;/div&gt;



&lt;p&gt;You can use any of the above-stated data types or a function as a key. But you should have proper reason and logic behind using them, mainly in the case of tuples and functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Values
&lt;/h3&gt;

&lt;p&gt;Unlike Keys, Values don't have any restrictions. They can be of any data type or even another dictionary or List. We can even use functions as a value associated with the dictionary key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; def fruits(): return ["apple", "oranges"]
&amp;gt;&amp;gt;&amp;gt; mydict = {"name": "Shantanu", "things": fruits()}
&amp;gt;&amp;gt;&amp;gt; mydict["things"]
["apple", "oranges"]

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

&lt;/div&gt;



&lt;p&gt;When we access the function from the dictionary using its associated key, the function is executed and returns the value from it instead of the function itself. That's why, above code prints list &lt;code&gt;["apple", oranges"]&lt;/code&gt; and not &lt;code&gt;fruits()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  CRUD operations with a dictionary
&lt;/h3&gt;

&lt;p&gt;Till now you got familiar with the dictionary, key, value and their properties. Now, it's time to see how dictionaries work. We will see basic CRUD (Create, Read, Update, Delete) operations on Dictionaries.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating a dictionary&lt;/li&gt;
&lt;li&gt;Reading/Accessing a dictionary&lt;/li&gt;
&lt;li&gt;Updating a dictionary&lt;/li&gt;
&lt;li&gt;Deleting a dictionary&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Creating dictionary
&lt;/h4&gt;

&lt;p&gt;You are already familiar with the syntax of the dictionary and have already created it in previous examples. The dictionary can be created using one or many key-value pairs separated by a comma and enclosed inside curly braces. Each key and its associated value is separated by a colon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; my_dict = {"name": "Shantanu Nighot", "age": 21, "grade": "A" , "pass": True }
{"name": "Shantanu Nighot", "age": 21, "grade": "A" , "pass": True }
&amp;gt;&amp;gt;&amp;gt; type(my_dict)
&amp;lt;class 'dict'&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;It's not necessary to create a dictionary with some data. We can also define an empty dictionary by either using empty curly braces &lt;code&gt;{}&lt;/code&gt; or the &lt;code&gt;dict()&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; dict1 = {}
{}
&amp;gt;&amp;gt;&amp;gt; dict2 = dict{}
{}
&amp;gt;&amp;gt;&amp;gt; print(type(dict1), type(dict2))
&amp;lt;class 'dict'&amp;gt; &amp;lt;class 'dict'&amp;gt;
&amp;gt;&amp;gt;&amp;gt; print(len(dict1), len(dict2))
0 0

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Reading/Accessing dictionary
&lt;/h4&gt;

&lt;p&gt;We can not use numerical indices in dictionaries as we do in lists. And if we do so, Python will raise an exception &lt;code&gt;KeyError&lt;/code&gt; as it can not find a key in the dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; mydict = {"fruit": "Mango"}
&amp;gt;&amp;gt;&amp;gt; mydict[0]
Traceback (most recent call last):
  File "&amp;lt;pyshell#13&amp;gt;", line 1, in &amp;lt;module&amp;gt;
    mydict[0]
KeyError: 0

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

&lt;/div&gt;



&lt;p&gt;In python dictionaries, instead of indices, keys are used to access values associated with them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; my_dict = {"name": "Shantanu Nighot", "age": 21, "grade": "A" , "pass": True }
&amp;gt;&amp;gt;&amp;gt; my_dict["name"]
Shantanu Nighot

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

&lt;/div&gt;



&lt;p&gt;From the nested dictionary, values of sublists or subdictionaries can be accessed using an extra index or a key just like in the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; fav_list = {"colors": ["Skyblue", "grey"], "number": 21, "fruits": ["Orange", "Mango", "Blueberries"]}
&amp;gt;&amp;gt;&amp;gt; fav_list["fruits"][1]
Mango
&amp;gt;&amp;gt;&amp;gt; fav_list["fruits"][0:2]
['Orange', 'Mango']

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

&lt;/div&gt;



&lt;p&gt;There are few built-in dictionary methods like items(), keys(), values(), and get(). They can also be used for accessing the data from the dictionary. We are going to learn about them later in this article.&lt;/p&gt;

&lt;p&gt;Now as you got familiar with accessing the data from the dictionaries, you can start working on using them with Python's operators and built-in functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13}
{ "num1": 10, "num2": 13}

&amp;gt;&amp;gt;&amp;gt; numbers["num1"] + numbers["num2"]
23

&amp;gt;&amp;gt;&amp;gt; "num2" in numbers
True

&amp;gt;&amp;gt;&amp;gt; sorted(mydict, reverse=True)
{ "num2": 13, "num1": 10}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Updating dictionary
&lt;/h4&gt;

&lt;p&gt;Any value in the dictionary can be changed by referencing its key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13}
&amp;gt;&amp;gt;&amp;gt; numbers["num1"] = 12
&amp;gt;&amp;gt;&amp;gt; numbers
{ "num1": 12, "num2": 13}

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

&lt;/div&gt;



&lt;p&gt;New data can be added using the same above syntax. If the key is present in the dictionary then Python reassigns a new value to a key and if a key is not found then it creates a new key and assigns value to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13}
&amp;gt;&amp;gt;&amp;gt; numbers["num3"] = 12
&amp;gt;&amp;gt;&amp;gt; numbers
{ "num1": 10, "num2": 13, "num3": 12}

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

&lt;/div&gt;



&lt;p&gt;There is a built-in dictionary method update() which is also used to update the data from a dictionary. We will learn about it later in this article.&lt;/p&gt;

&lt;h4&gt;
  
  
  Deleting dictionary
&lt;/h4&gt;

&lt;p&gt;The del function can be used to delete the whole dictionary. After that, any reference to it will throw a NameError.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13}
&amp;gt;&amp;gt;&amp;gt; del(numbers)
&amp;gt;&amp;gt;&amp;gt; numbers["num1"]
File "main.py", line 3, in &amp;lt;module&amp;gt;
    numbers["num1"]
NameError: name 'fav_list' is not defined

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

&lt;/div&gt;



&lt;p&gt;If you do not want to delete the dictionary and just want to delete data in it, then you can assign an empty dictionary to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13}
{ "num1": 10, "num2": 13}

&amp;gt;&amp;gt;&amp;gt; numbers = {}
{}

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

&lt;/div&gt;



&lt;p&gt;There are few built-in dictionary methods used to perform deleting operations on dictionaries. We are going to learn about them in the next topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Built-in dictionary methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
items() &lt;/li&gt;
&lt;li&gt;
keys() &lt;/li&gt;
&lt;li&gt;
values() &lt;/li&gt;
&lt;li&gt;
get() &lt;/li&gt;
&lt;li&gt;
update() &lt;/li&gt;
&lt;li&gt;
clear() &lt;/li&gt;
&lt;li&gt;
pop() &lt;/li&gt;
&lt;li&gt;
popitem() &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  .items()
&lt;/h4&gt;

&lt;p&gt;This method returns the list of tuples containing key-value pairs from a dictionary. The first element of each tuple is the key and the second element is the value associated with that key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; numbers.items()
dict_items([('num2', 13), ('num3', 23), ('num1', 10)])
&amp;gt;&amp;gt;&amp;gt; type(numbers.items())
&amp;lt;class 'dict_items'&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;You can see that the method returns the data of type "dict_items". Hence, we need to convert it into a list so that we can access them using normal indexing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; list(numbers.items())
[('num2', 13), ('num3', 23), ('num1', 10)]

&amp;gt;&amp;gt;&amp;gt; list(numbers.items())[0]
('num2', 13)
&amp;gt;&amp;gt;&amp;gt; list(numbers.items())[1][1]
13

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

&lt;/div&gt;



&lt;p&gt;Mostly, the items() method is used while working with loops and dictionaries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; for k, v in numbers.items(): print(k, v)
num2 13
num3 23
num1 10

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  .keys()
&lt;/h4&gt;

&lt;p&gt;This method returns the list of all keys in the dictionary. The return type is "dict_keys", hence we need to convert it into a list using list() so that we can use normal indexing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; list(numbers.keys())
['num1', 'num2', 'num3']

&amp;gt;&amp;gt;&amp;gt; for key in numbers.keys(): print(numbers[key])
10
13
23

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  .values()
&lt;/h4&gt;

&lt;p&gt;This method returns the list of all values in the dictionary. The return type is "dict_values", hence we need to convert it into a list for using normal indexing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; list(numbers.values())
[10, 13, 23]

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  .get(key, default_value)
&lt;/h4&gt;

&lt;p&gt;This method returns the value associated with the key if it's present in the dictionary else returns None. This method does not raise KeyError if the key is not found in the dictionary and returns the default value which is None by default.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; numbers.get("num3")
23
&amp;gt;&amp;gt;&amp;gt; numbers.get("num4")
None

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

&lt;/div&gt;



&lt;p&gt;The second argument is optional but can be very helpful when we want to return a specific value when the key is not found.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; numbers.get("num5", 0)
0

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  .update()
&lt;/h4&gt;

&lt;p&gt;This method is used to update existing data and to add new entries to the dictionary. There are several ways of using the update() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; numbers.update({"num2": 5, "num4": 10})
&amp;gt;&amp;gt;&amp;gt; numbers
{'num1': 10, 'num2': 5, 'num3': 23, 'num4': 10}

&amp;gt;&amp;gt;&amp;gt; numbers.update([("num5", 4), ("num2", 11)])
&amp;gt;&amp;gt;&amp;gt; numbers
{'num1': 10, 'num2': 11, 'num3': 23, 'num4': 10, 'num5': 4}

&amp;gt;&amp;gt;&amp;gt; numbers.update(num1=0, num6=2)
&amp;gt;&amp;gt;&amp;gt; numbers
{'num1': 0, 'num2': 11, 'num3': 23, 'num4': 10, 'num5': 4, 'num6': 2}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  .clear()
&lt;/h4&gt;

&lt;p&gt;This method is used to remove all key-value pairs from the dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; numbers.clear()
&amp;gt;&amp;gt;&amp;gt; numbers
{}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  .pop(key, default_value)
&lt;/h4&gt;

&lt;p&gt;This method returns the value associated with the key and removes the key-value pair from the dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; numbers.pop("num2")
13
&amp;gt;&amp;gt;&amp;gt; numbers
{ "num1": 10, "num3": 23}

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

&lt;/div&gt;



&lt;p&gt;If the key is not present in the dictionary then python raises an exception KeyError. Hence, it is recommended to provide default value as a second argument. This default value will be returned if the key is not found in the dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; numbers.pop("num4", 0)
0
&amp;gt;&amp;gt;&amp;gt; numbers
{ "num1": 10, "num2": 13, "num3": 23}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  .popitem()
&lt;/h4&gt;

&lt;p&gt;This method returns the last key-value pair as a tuple and removes it from the dictionary. If the dictionary is empty, it raises an exception KeyError.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; numbers = { "num1": 10, "num2": 13, "num3": 23}
&amp;gt;&amp;gt;&amp;gt; numbers.popitem()
('num3', 23)
&amp;gt;&amp;gt;&amp;gt; numbers
{ "num1": 10, "num2": 13}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python dictionary is the collection where data is store in the form of key-value pairs.&lt;/li&gt;
&lt;li&gt;Python dictionaries are immutable, ordered and can be nested.&lt;/li&gt;
&lt;li&gt;Keys must be of immutable data type whereas values can be of any data type.&lt;/li&gt;
&lt;li&gt;Functions and few python keywords can also be used as keys and values.&lt;/li&gt;
&lt;li&gt;Few built-in dictionary methods make it easier to work with dictionaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have also created a Jupyter notebook for this article on GitHub. This notebook will help you run the above codes effectively. Check it out by clicking &lt;a href="https://github.com/magbanum/Learn-Python/blob/master/Python-Dictionaries.ipynb"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope you found this article helpful. Do comment below if I missed something to make this thread more informative.&lt;/p&gt;

&lt;p&gt;You can also sign up for the newsletter to get my articles directly in your inbox.&lt;/p&gt;

&lt;p&gt;See you in the next article.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be better than yesterday's you.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>dictionary</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Python program to make text URL safe.</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Thu, 09 Sep 2021 07:24:39 +0000</pubDate>
      <link>https://dev.to/magbanum/python-program-to-make-text-url-safe-274</link>
      <guid>https://dev.to/magbanum/python-program-to-make-text-url-safe-274</guid>
      <description>&lt;p&gt;Liquid syntax error: Variable '{{% raw %}' was not properly terminated with regexp: /\}\}/&lt;/p&gt;
</description>
      <category>python</category>
      <category>url</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Starting Django project with MongoDB using Djongo.</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Thu, 22 Jul 2021 05:55:06 +0000</pubDate>
      <link>https://dev.to/magbanum/starting-django-project-with-mongodb-using-djongo-om1</link>
      <guid>https://dev.to/magbanum/starting-django-project-with-mongodb-using-djongo-om1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iN4_mt0D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://magbanum.tech/_next/image%3Furl%3Dhttps%253A%252F%252Fcdn.hashnode.com%252Fres%252Fhashnode%252Fimage%252Fupload%252Fv1626904402218%252F25Lz_xlbP.png%253Fw%253D1600%2526h%253D840%2526fit%253Dcrop%2526crop%253Dentropy%2526auto%253Dcompress%252Cformat%2526format%253Dwebp%26w%3D1920%26q%3D75" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iN4_mt0D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://magbanum.tech/_next/image%3Furl%3Dhttps%253A%252F%252Fcdn.hashnode.com%252Fres%252Fhashnode%252Fimage%252Fupload%252Fv1626904402218%252F25Lz_xlbP.png%253Fw%253D1600%2526h%253D840%2526fit%253Dcrop%2526crop%253Dentropy%2526auto%253Dcompress%252Cformat%2526format%253Dwebp%26w%3D1920%26q%3D75" alt="Starting Django project with MongoDB using Djongo"&gt;&lt;/a&gt;&lt;br&gt;
Hi everyone, as an enthusiast developer, I wanted to learn about databases. There are lots of them on the internet but the one that caught my attention is MongoDB. Recently I heard about the unique database called MongoDB. It's a NoSQL document database where data is stored in documents instead of rows and columns as in SQL databases.&lt;/p&gt;

&lt;p&gt;After researching more about MongoDB, its features and applications, I started learning about it and also completed my first Basic course at MongoDB University. Thanks to GitHub's &lt;a href="https://education.github.com/pack"&gt;Student Developer Pack&lt;/a&gt; and &lt;a href="https://www.mongodb.com/students/"&gt;MongoDB&lt;/a&gt; to provide free access to MongoDB University courses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/posts/shantanu-nighot_my-mongodb-university-course-proof-of-completion-activity-6820338711700520960-50C1"&gt;https://www.linkedin.com/posts/shantanu-nighot_my-mongodb-university-course-proof-of-completion-activity-6820338711700520960-50C1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With basic knowledge of the MongoDB database, I decided to use it with one of my projects and here is what I have built.&lt;/p&gt;

&lt;p&gt;Quotes gen project: &lt;a href="https://quotes-gen-project.herokuapp.com"&gt;https://quotes-gen-project.herokuapp.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this project, I have created a model to store quotes and authors in a MongoDB database and View to display a random quote from a collection of more than 2 lacs of quotes.&lt;/p&gt;

&lt;p&gt;So, I am writing this article to share with you the steps I followed to connect the MongoDB database with my Django project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are currently 3 ways to connect the MongoDB database to Django, using&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://pymongo.readthedocs.io/"&gt;PyMongo&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="http://mongoengine.org/"&gt;MongoEngine&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.djongomapper.com/"&gt;Djongo&lt;/a&gt; [&lt;strong&gt;Dj&lt;/strong&gt; ango + M &lt;strong&gt;ongo&lt;/strong&gt;]&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I personally loved how &lt;strong&gt;Djongo&lt;/strong&gt; manages to connect &lt;strong&gt;MongoDB&lt;/strong&gt; to &lt;strong&gt;Django&lt;/strong&gt; with minimum changes. I will suggest using Djongo for beginners as it does not change the original Django ORM and everything is kept minimal and simple. And in this article, we are going to use it to connect Django with MongoDB.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eESMxztO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626929185875/YRFLkKe1M.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eESMxztO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626929185875/YRFLkKe1M.png" alt="Djongo logo"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Pre-requisites
&lt;/h4&gt;

&lt;p&gt;Before proceeding further make sure you have basic knowledge of how things work in Django and MongoDB. I have mentioned few resources to get started with them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.djangoproject.com/en/3.2/intro/tutorial01/"&gt;Writing first Django app, part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://realpython.com/django-setup/"&gt;Your First Steps With Django: Set Up a Django Project&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.mongodb.com/basics"&gt;MongoDB Basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.mongodb.com/basics/create-database"&gt;Create a Database in MongoDB&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get started,&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 1: Virtual environment setup
&lt;/h4&gt;

&lt;p&gt;Make sure you have created a virtual environment and activates it using the following commands,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Install virtualenv package
python -m pip install --user virtualenv

# Create a virtual environment with the name 'env'
python -m venv env

# Activate the virtual environment
env\Scripts\activate

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 2: Install packages
&lt;/h4&gt;

&lt;p&gt;Install the required packages like Django and Djongo using the following command,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install Django Djongo pymongo[srv]

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 3: Start the Django project
&lt;/h4&gt;

&lt;p&gt;Now as our virtual env is activated, let's create our Django project and the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Start the Django project
django-admin startproject &amp;lt;project-name&amp;gt;

# Open the project folder
cd &amp;lt;project-name&amp;gt;

# Create an app
python manage.py startapp &amp;lt;app-name&amp;gt;

# Start the server to see the project running
python manage.py runserver

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

&lt;/div&gt;



&lt;p&gt;Don't forget to add the newly created app to &lt;code&gt;INSTALLED_APPS&lt;/code&gt; in the &lt;code&gt;settings.py&lt;/code&gt; file.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4: MongoDB cluster creation
&lt;/h4&gt;

&lt;p&gt;Now our app is ready, let's head to MongoDB to create our Atlas cluster.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;a href="https://cloud.mongodb.com"&gt;https://cloud.mongodb.com&lt;/a&gt; and sign in&lt;/li&gt;
&lt;li&gt;Create an Organisation and a Project&lt;/li&gt;
&lt;li&gt;Create Cluster

&lt;ul&gt;
&lt;li&gt;Click on &lt;strong&gt;Create a Cluster&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Select the Shared Database deployment for this tutorial&lt;/li&gt;
&lt;li&gt;Change the Cluster name&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create&lt;/strong&gt; button (Takes few minutes to complete the process)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Connect&lt;/strong&gt; and Create a Database User with Username and Password. If it asks to add an IP address then Click on &lt;strong&gt;Add IP address&lt;/strong&gt; without adding anything. This allows access to your cluster from any IP address and is not recommended when creating a real-life project.&lt;/li&gt;
&lt;li&gt;Then select the connection type &lt;strong&gt;Connect your application&lt;/strong&gt; and copy the provided link. We will need it later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For more information, visit the MongoDB Documentation, &lt;a href="https://docs.atlas.mongodb.com/getting-started/"&gt;Get started with Atlas&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 5: Database information in Django
&lt;/h4&gt;

&lt;p&gt;Everything is ready to be served. In &lt;code&gt;settings.py&lt;/code&gt; file add the following information in &lt;code&gt;DATABASE&lt;/code&gt; and replace the credentials like &lt;code&gt;username&lt;/code&gt;, &lt;code&gt;password&lt;/code&gt;, and &lt;code&gt;atlas cluster&lt;/code&gt; with yours. You can add the database name of your choice, then Django will create that database on migration. Add &lt;code&gt;admin&lt;/code&gt; instead of &lt;code&gt;&amp;lt;myFirstDatabase&amp;gt;&lt;/code&gt;.Remember the link we copied earlier, you can also use it in place of the given link.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASES = {
        'default': {
            'ENGINE': 'djongo',
            'NAME': 'your-db-name',
            'ENFORCE_SCHEMA': False,
            'CLIENT': {
                'host': 'mongodb+srv://&amp;lt;username&amp;gt;:&amp;lt;password&amp;gt;@&amp;lt;atlas cluster&amp;gt;/&amp;lt;myFirstDatabase&amp;gt;?retryWrites=true&amp;amp;w=majority'
            }  
        }
}

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

&lt;/div&gt;



&lt;p&gt;I may have skipped some Django parts to focus this article on MongoDB connection. Please make sure you have followed all steps required to run a Django project.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 6: Where the magic happens...
&lt;/h4&gt;

&lt;p&gt;It's time to actually connect our Django project to a MongoDB cluster. Run the following commands to make migrations and migrate the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Make migrations
python manage.py makemigrations

# Migrate
python manage.py migrate

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

&lt;/div&gt;



&lt;p&gt;And that's it. You can see in your MongoDB cluster that the new database is created with the name provided by you and few collections inside that Database like the following,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d31k_55e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626895651289/XVrffVD5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d31k_55e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626895651289/XVrffVD5u.png" alt="Database collections"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you get an error &lt;code&gt;djongo.database.DatabaseError&lt;/code&gt; while migrating the database, run the following command &lt;code&gt;pip install sqlparse==0.2.4&lt;/code&gt; to fix the issues.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Step 7: Creating Models
&lt;/h4&gt;

&lt;p&gt;To know how Django models are connected to the MongoDB database, let's create a model in our project. For example, in the following code, the model &lt;code&gt;People&lt;/code&gt; is created with &lt;code&gt;Name&lt;/code&gt;, &lt;code&gt;Age&lt;/code&gt; fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class People(models.Model):
    Name = models.CharField(max_length=100, default=None)
    Age = models.IntegerField(default=None)

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

&lt;/div&gt;



&lt;p&gt;Don't forget to register the model by adding the following lines in the &lt;code&gt;admin.py&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from . models import People
admin.site.register(People)

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

&lt;/div&gt;



&lt;p&gt;Again run &lt;code&gt;python manage.py makemigrations&lt;/code&gt; and &lt;code&gt;python manage.py migrate&lt;/code&gt; to migrate the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nnT5PhDC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626902574980/_OooIFOhIa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nnT5PhDC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626902574980/_OooIFOhIa.png" alt="MongoDB Collections"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the MongoDB cluster, you can see that the new collection &lt;code&gt;testapp_people&lt;/code&gt; is created. &lt;strong&gt;In Django, the models are connected to the MongoDB collections with the name &lt;code&gt;&amp;lt;app-name&amp;gt;_&amp;lt;model-name&amp;gt;&lt;/code&gt; where &lt;code&gt;app-name&lt;/code&gt; is the name of Django app and &lt;code&gt;model-name&lt;/code&gt; is the name of the model inside that app&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 8: Adding data
&lt;/h4&gt;

&lt;p&gt;Last but not the least, let's create a superuser using the following command and add some data using Django admin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py createsuperuser

# To start the server
python manage.py runserver

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

&lt;/div&gt;



&lt;p&gt;After adding some data from the admin panel, you can see that the collection is crowded with some data which are called Documents. MongoDB stores the data in the form of documents.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--017mYTL3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626900461271/McZ8lhJX-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--017mYTL3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626900461271/McZ8lhJX-.png" alt="MongoDB collections"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is all you need to know while starting a Django project with MongoDB. Now go ahead and create some interesting projects. Check out &lt;a href="https://quotes-gen-project.herokuapp.com"&gt;GitHub repository&lt;/a&gt; for the project I mentioned in the start to help you in the process.&lt;/p&gt;

&lt;p&gt;If you face any issues in the process, don't hesitate to mention them in a comment. I am always peeping at my inbox for new comments and will get back to you immediately.&lt;/p&gt;

&lt;p&gt;If you have any suggestions or feedback related to my articles, please let me know.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article. I hope this was helpful to you.🌻&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Octoprofile - the Django project</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Mon, 28 Jun 2021 13:47:13 +0000</pubDate>
      <link>https://dev.to/magbanum/octoprofile-the-django-project-43oj</link>
      <guid>https://dev.to/magbanum/octoprofile-the-django-project-43oj</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qfVm_Is3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9owqjkwf7x1i563pu2k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qfVm_Is3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9owqjkwf7x1i563pu2k.png" alt="Octoprofile"&gt;&lt;/a&gt;&lt;br&gt;
I am very happy to share with you the Django project I have been working on last 20 days, it's called an Octoprofile. Project Octoprofile displays the GitHub profile in a better way with Charts and sortable lists.&lt;/p&gt;

&lt;p&gt;Octoprofile collects the user information on GitHub using GitHub API endpoints and then Django's function-based view passes it to the template after manipulating the data.&lt;/p&gt;

&lt;p&gt;To view your Octoprofile visit &lt;a href="https://octoprofile.herokuapp.com/"&gt;octoprofile.herokuapp.com&lt;/a&gt; and enter your GitHub username. Don't have a GitHub profile, check mine by typing &lt;code&gt;magbanum&lt;/code&gt; or by clicking &lt;a href="https://octoprofile.herokuapp.com/profile/?username=magbanum"&gt;here&lt;/a&gt;. The website might be slower for the first load as the project is deployed on Heroku's free dyno.&lt;/p&gt;

&lt;p&gt;I have worked a lot on the coding and researching part as I had to make everything from scratch. As I didn't know JavaScript I faced problems in the beginning while sorting the list and displaying the charts. But with the basics of JavaScript and few JavaScript libraries (ChartJS &amp;amp; ListJS), I was able to make them all work.&lt;/p&gt;

&lt;h4&gt;
  
  
  About the project
&lt;/h4&gt;

&lt;p&gt;I have created an app inside the Django project called &lt;code&gt;profiles&lt;/code&gt; and created two templates &lt;code&gt;home.html&lt;/code&gt; for the landing page and &lt;code&gt;profile_page.html&lt;/code&gt; for the profile page.&lt;/p&gt;

&lt;p&gt;The function-based views &lt;code&gt;get_username&lt;/code&gt; and &lt;code&gt;get_repodata&lt;/code&gt; are used to collect and manipulate the data and pass it to the template &lt;code&gt;profile_page.html&lt;/code&gt;. The class-based API views &lt;code&gt;TopLanguages&lt;/code&gt;, &lt;code&gt;MostStarred&lt;/code&gt;, and &lt;code&gt;StarsPerLanguages&lt;/code&gt; are used to create API endpoints for top languages by the user, most starred repositories of the user, and stars per languages respectively. Check out &lt;a href="https://github.com/magbanum/octoprofile"&gt;GitHub repository&lt;/a&gt; for source code.&lt;/p&gt;

&lt;p&gt;ChartJS uses these endpoints and displays charts accordingly. The project is then deployed on the Heroku (free dyno) using Heroku CLI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Few project features.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Clean UI&lt;/li&gt;
&lt;li&gt;Responsive design&lt;/li&gt;
&lt;li&gt;Easy navigation&lt;/li&gt;
&lt;li&gt;Graphical view&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Coded with
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;li&gt;HTML5&lt;/li&gt;
&lt;li&gt;CSS3&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Built with
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.djangoproject.com/"&gt;Django&lt;/a&gt; High-level Python Web framework&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.django-rest-framework.org/"&gt;Django REST framework&lt;/a&gt; Powerful and flexible toolkit for building Web APIs.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.chartjs.org/"&gt;ChartJS&lt;/a&gt; Simple yet flexible JavaScript charting for designers &amp;amp; developers&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://listjs.com/"&gt;ListJS&lt;/a&gt; Perfect library for adding search, sort, filters to tables, lists and various HTML elements.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.github.com/en/rest"&gt;GitHub API&lt;/a&gt; Create calls to get the data you need to integrate with GitHub.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://jquery.com/"&gt;Jquery&lt;/a&gt; Fast, small, and feature-rich JavaScript library.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Deployed on
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://heroku.com/"&gt;Heroku&lt;/a&gt; Platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall this project was really amazing experience and taught me a lot of things in the development process. I will love to see you acknowledge my efforts by visiting &lt;a href="https://octoprofile.herokuapp.com/"&gt;octoprofile.herokuapp.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;How's that?&lt;/p&gt;

&lt;p&gt;Any suggestions and feedbacks are always welcomed. Bugs are not welcomed but you can definitely let me know if you found one. Visit the project repository &lt;a href="https://github.com/magbanum/octoprofile"&gt;here&lt;/a&gt; and give it a ⭐ if you liked your Octoprofile and share it with everyone.&lt;/p&gt;

&lt;p&gt;Click &lt;a href="http://twitter.com/intent/tweet?text=I'm%20in%20love%20with%20my%20Octoprofile.%20See%20yours%3A&amp;amp;url=http%3A%2F%2Foctoprofile.heokuapp.com%2F&amp;amp;via=magbanum"&gt;here&lt;/a&gt; to share my project on Twitter.&lt;/p&gt;

&lt;p&gt;Thank you for taking the time to read this article.🌻&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be better than yesterday's YOU!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>showdev</category>
      <category>django</category>
      <category>github</category>
      <category>chartjs</category>
    </item>
    <item>
      <title>Pagination in Django.</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Wed, 23 Jun 2021 15:33:54 +0000</pubDate>
      <link>https://dev.to/magbanum/pagination-in-django-56g8</link>
      <guid>https://dev.to/magbanum/pagination-in-django-56g8</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.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624460987792%2FY6CABpoIe.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.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624460987792%2FY6CABpoIe.png" alt="Pagination in Django"&gt;&lt;/a&gt;&lt;br&gt;
Pagination is the process of splitting the contents of a website, or a section of contents from a website, into discrete pages. One should use pagination to make his/her website more efficient to use when working with a huge list of contents for example "List of 100 blog posts". Also, paginating the content helps websites from being overcrowded with the content and make visitors focus on small content at a time. This increases the readability and visitors are likely to return to your website again. &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624461433050%2FLOAsFmeSk.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.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624461433050%2FLOAsFmeSk.png" alt="Screenshot (63).png"&gt;&lt;/a&gt;Django has a pre-built class called "Paginator" to create and manage the paginated data. It can be imported by the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.core.paginator import Paginator

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

&lt;/div&gt;



&lt;p&gt;In Django, views are of two types which are Function-based views and Class-based views. Both types of views have different ways of paginating the content but the overall working is the same.&lt;/p&gt;

&lt;p&gt;For this tutorial, we will be using my project called "One for all Blogging platform". For the full source code of my project visit the &lt;a href="https://github.com/magbanum/oneforall-blog/" rel="noopener noreferrer"&gt;oneforall-blog&lt;/a&gt; GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/magbanum/oneforall-blog/" rel="noopener noreferrer"&gt;https://github.com/magbanum/oneforall-blog/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this project, I have created an app named &lt;code&gt;blog&lt;/code&gt; which has the function-based view "newsview" with the following code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;blog/views.py

def newsview(request):
    blogs = get_news()
    return render(request, 'blog/news_posts.html',{'blogs':blogs})

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;newsview&lt;/code&gt; function collects data from another function and passes it to template named &lt;code&gt;news_posts.html&lt;/code&gt; and then template displays that data. The HTML code looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;blog/template/blog/news_posts.html

 {% for blog in blogs %}
  &amp;lt;div class="col"&amp;gt;
        ....
        &amp;lt;h5 class="mb-1"&amp;gt;{{ blog.title }}&amp;lt;/h5&amp;gt;
        ....
  &amp;lt;/div&amp;gt;
{% endfor %}

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

&lt;/div&gt;



&lt;p&gt;Currently, the &lt;code&gt;newsview&lt;/code&gt; and the &lt;code&gt;news_posts.html&lt;/code&gt; template displays all the items stored in the blogs on a single page. So we need to add some code to paginate the items.&lt;/p&gt;

&lt;p&gt;Let's first import the &lt;code&gt;Paginator&lt;/code&gt; using the following line of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.core.paginator import Paginator

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

&lt;/div&gt;



&lt;p&gt;In function-based views, we need to specify the data which we want to paginate and the number of items by which we want to paginate the data. For the &lt;code&gt;newsview&lt;/code&gt; we want to paginate the contents of &lt;code&gt;blogs&lt;/code&gt; by 6 items. Also, in the previous code, we returned the &lt;code&gt;blogs&lt;/code&gt; to the template but now we will need to return paginated data that is &lt;code&gt;page_obj&lt;/code&gt;. The final code will become,&lt;/p&gt;

&lt;p&gt;Project directory: &lt;a href="https://github.com/magbanum/oneforall-blog/blob/main/blog/views.py" rel="noopener noreferrer"&gt;blog/views.py&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.core.paginator import Paginator

def newsview(request):
    blogs = get_news()
    paginator = Paginator(blogs, 6)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    return render(request, 'blog/news_posts.html',{'page_obj':page_obj})

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

&lt;/div&gt;



&lt;p&gt;Now that we have paginated the data successfully we need to display it in our &lt;code&gt;news_posts.html&lt;/code&gt;template. Also, we have to add a navigation panel for the visitors to navigate between the pages. But not to worry, there are few lines of code to include simple navigation with the links for the first, previous, next and last page. So the final template will look like this,&lt;/p&gt;

&lt;p&gt;Project directory: &lt;a href="https://github.com/magbanum/oneforall-blog/blob/main/blog/templates/blog/news_posts.html" rel="noopener noreferrer"&gt;blog/template/blog/news_posts.html&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% for blog in page_obj %}
  &amp;lt;div class="col"&amp;gt;
        ....
        &amp;lt;h5 class="mb-1"&amp;gt;{{ blog.title }}&amp;lt;/h5&amp;gt;
        ....
  &amp;lt;/div&amp;gt;
{% endfor %}

&amp;lt;!-- For page navigation links --&amp;gt;
&amp;lt;div class="pagination" style="width: fit-content;"&amp;gt;
    &amp;lt;span class="step-links"&amp;gt;
    {% if page_obj.has_previous %}
        &amp;lt;a href="?page=1" class="page-link" style="display: inline;"&amp;gt;&amp;amp;laquo; first&amp;lt;/a&amp;gt;
        &amp;lt;a href="?page={{ page_obj.previous_page_number }}" class="page-link" style="display: inline;"&amp;gt;previous&amp;lt;/a&amp;gt;
    {% endif %}

    &amp;lt;span class="current"&amp;gt;
        Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
    &amp;lt;/span&amp;gt;

    {% if page_obj.has_next %}
        &amp;lt;a href="?page={{ page_obj.next_page_number }}" class="page-link" style="display: inline;"&amp;gt;next&amp;lt;/a&amp;gt;
        &amp;lt;a href="?page={{ page_obj.paginator.num_pages }}" class="page-link" style="display: inline;"&amp;gt;last &amp;amp;raquo;&amp;lt;/a&amp;gt;
    {% endif %}
    &amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;And that's it. After running the server you will get nicely organised content with the navigation links to move between the pages.&lt;/p&gt;

&lt;p&gt;| Before Pagination | After Pagination |&lt;br&gt;
| &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624455753454%2Fl6y6M1JTz.gif" 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.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624455753454%2Fl6y6M1JTz.gif" alt="Website before paginating"&gt;&lt;/a&gt; | &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624458670671%2FwFQ0YHenv.gif" 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.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624458670671%2FwFQ0YHenv.gif" alt="Website after paginating"&gt;&lt;/a&gt; |&lt;/p&gt;

&lt;p&gt;In the case of Class-based Listview, we don't need to import the &lt;code&gt;Paginator&lt;/code&gt; as &lt;code&gt;ListView&lt;/code&gt; has its own way of paginating the List. All we have to do is specify the number by which we want to paginate the list. The project "One for all Blogging system" also have a class-based view named &lt;code&gt;HomeView&lt;/code&gt; which is paginated by 6 items.&lt;/p&gt;

&lt;p&gt;Project directory: &lt;a href="https://github.com/magbanum/oneforall-blog/blob/main/blog/views.py" rel="noopener noreferrer"&gt;blog/views.py&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.views.generic import ListView

class HomeView(ListView):
    paginate_by = 6
    model = Post
    template_name = 'blog/home.html'
    ordering = ['-post_date']

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

&lt;/div&gt;



&lt;p&gt;The remaining steps are the same as in the function-based view. We can add navigation links just like we did with our &lt;code&gt;newsview&lt;/code&gt;. You can see the code for template &lt;code&gt;home.html&lt;/code&gt;&lt;a href="https://github.com/magbanum/oneforall-blog/blob/main/blog/templates/blog/home.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Visit the One for all Blogging platform at &lt;a href="https://oneforall-blog.herokuapp.com/" rel="noopener noreferrer"&gt;https://oneforall-blog.herokuapp.com/&lt;/a&gt; to see the working of our code.&lt;/p&gt;

&lt;p&gt;And that's all about the Pagination in Django. For the full source code of my project visit the GitHub repo &lt;a href="https://github.com/magbanum/oneforall-blog/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and give a star if you like my work.&lt;/p&gt;

&lt;p&gt;I hope this article was helpful for you. Please do subscribe to the newsletter to get my blog posts directly in your mailbox📬.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>documentation</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A virtual environment in Powershell.</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Mon, 31 May 2021 06:54:43 +0000</pubDate>
      <link>https://dev.to/magbanum/a-virtual-environment-in-powershell-2mnm</link>
      <guid>https://dev.to/magbanum/a-virtual-environment-in-powershell-2mnm</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n-HNDYZX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80b1a8r1rs46ej0s7lfu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n-HNDYZX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80b1a8r1rs46ej0s7lfu.png" alt="a virtual environment in powershell"&gt;&lt;/a&gt;&lt;br&gt;
The virtual environment is used to create an isolated environment for the different projects. This helps developers to keep the dependencies required by projects separate. So let's learn basic commands to create and activate the virtual env and install packages on it.&lt;/p&gt;
&lt;h4&gt;
  
  
  Prerequisites,
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;PIP&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Install &lt;code&gt;virtualenv&lt;/code&gt; package using the following command,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pip install --user virtualenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go to the folder where you want to create a virtual environment and run the following command to create a virtual environment with the name "env". This will create the folder named "env" in a directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m venv env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can name it whatever you what just make it logical and relative to your project.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To activate the virtual environment, go to the directory where you installed it and run the following command by replacing "env" with your environment name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;env\Scripts\activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To install any package make sure the virtual environment is active. Use the following command to install the package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To install multiple packages use a comma &lt;code&gt;,&lt;/code&gt; to separate package names. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install requests, Django, BeautifulSoup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To install packages using &lt;code&gt;requirements.txt&lt;/code&gt; file, use the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get package names installed on a virtual environment into a &lt;code&gt;requirements.txt&lt;/code&gt; file use the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To deactivate the env use,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deactivate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am a total beginner and might have missed command that I never used before. So, please comment below few commands that are helpful for developers. Thanks for reading and do follow for more such articles.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Never stop learning!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>powershell</category>
      <category>developer</category>
      <category>package</category>
    </item>
    <item>
      <title>How to use Hashnode APIs in Python.</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Mon, 17 May 2021 09:58:39 +0000</pubDate>
      <link>https://dev.to/magbanum/how-to-use-hashnode-apis-in-python-5b2f</link>
      <guid>https://dev.to/magbanum/how-to-use-hashnode-apis-in-python-5b2f</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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiv08jpsjwndqxtypnczr.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiv08jpsjwndqxtypnczr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The Hashnode GraphQL APIs public beta was released in 2019 to allow the users to create apps based on it. Even if it was minimal release, it has a lot of queries and mutations that can be used.&lt;/p&gt;

&lt;p&gt;Today we are going to learn how to use those Hashnode API queries in python to get the most out of it.&lt;/p&gt;

&lt;p&gt;Steps for creating a python program to get the user details with Hashnode GraphQL API:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create query in GraphQL&lt;/li&gt;
&lt;li&gt;Add queries and headers in the program&lt;/li&gt;
&lt;li&gt;Create a function to run the JSON Query&lt;/li&gt;
&lt;li&gt;Get the output as JSON Dictionary&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some prerequisites are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make sure you have the Requests module installed on your system using &lt;code&gt;pip install requests&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You will need to create/revote the Personal Access Token for the Authentication purpose. You can find it at &lt;a href="https://hashnode.com/settings/developer" rel="noopener noreferrer"&gt;Hashnode Developer settings&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Personal Access Token is like a private key to your Hashnode account. You can use a personal access token to interact with your Hashnode account using our APIs. Never disclose your access tokens and always keep them private. You can revoke the generated tokens at any time. &lt;a class="mentioned-user" href="https://dev.to/hashnode"&gt;@hashnode&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Creating queries in GraphQL
&lt;/h3&gt;

&lt;p&gt;In this tutorial, we are going to get user details like &lt;code&gt;name&lt;/code&gt;,&lt;code&gt;tagline&lt;/code&gt;,&lt;code&gt;numFollowers&lt;/code&gt;,&lt;code&gt;publicationDomain&lt;/code&gt;, etc. So we will need to create a query for those in GraphQL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query {
  user(username: "magbanum") {
    username
    name
    tagline
    numFollowers
    publicationDomain
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add more properties like &lt;code&gt;numFollowings&lt;/code&gt;,&lt;code&gt;numReactions&lt;/code&gt;, &lt;code&gt;location&lt;/code&gt;,&lt;code&gt;photo&lt;/code&gt;,&lt;code&gt;coverImage&lt;/code&gt; with the help of DOCS and SCHEMA available at &lt;a href="https://api.hashnode.com/" rel="noopener noreferrer"&gt;Hashnode GraphQL API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For now, let's stick to this query. You can replace "magbanum" with your own username to get personalised results.&lt;/p&gt;

&lt;p&gt;Visit &lt;a href="https://api.hashnode.com/" rel="noopener noreferrer"&gt;https://api.hashnode.com/&lt;/a&gt; and paste the above query there and click the play button to run the script. The output will look like this,&lt;/p&gt;

&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.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621191480661%2FkM1fJWDH4.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.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621191480661%2FkM1fJWDH4.png" alt="Hashnode GraphQL API"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Query and Headers in Python program
&lt;/h3&gt;

&lt;p&gt;You have now created your first query in GraphQL, let's use it in our python program.&lt;/p&gt;

&lt;p&gt;Create a Python file named "hashnode-user.py", you can name it whatever you want but let's stick to "hashnode-user.py" for this tutorial.&lt;/p&gt;

&lt;p&gt;Firstly import the &lt;code&gt;requests&lt;/code&gt; module, by adding the following line,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember the Personal Access Token which you created earlier in prerequisites, we will need it now. Authorization header with the Personal access token is needed to run the queries or mutations hence we will pass them to the variable named &lt;code&gt;headers&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;headers = {
    "Authorization": "your_personal_access_token"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create the Variable named &lt;code&gt;query&lt;/code&gt; to store the query that we created in GraphQL as a multi-line string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query = """{
  user(username: "magbanum") {
    username
    name
    tagline
    numFollowers
    publicationDomain
  }
}"""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function to run JSON Query
&lt;/h3&gt;

&lt;p&gt;We have now reached the final step of our tutorial. As of now we have created a query and added it to our program. In this step, we will create a function that takes query as an argument and returns the output in JSON format.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a function named &lt;code&gt;run_query&lt;/code&gt; which takes the query and the headers we created as arguments.&lt;/li&gt;
&lt;li&gt;Now post a request in a variable named &lt;code&gt;response&lt;/code&gt; with the Hashnode API URL, query and headers as parameters.&lt;/li&gt;
&lt;li&gt;If ... else statement is used to return the JSON output if status code is 200 else raise the exception with status code. Learn more about status codes in HTTP requests &lt;a href="https://httpstatuses.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The function will look like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def run_query(query, headers):
    response = requests.post(url="https://api.hashnode.com", json={'query': query}, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception("Query failed to run by returning code of {}. {}".format(response.status_code, query))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, we need to call our function and get the data from it. Add few lines in the program but outside the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;result = run_query(query, headers)
print(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we are all done. The final code will look like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import requests

headers = {
    "Authorization": "your_personal_access_token"
}

def run_query(query, headers):
    response = requests.post(url="https://api.hashnode.com", json={'query': query}, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception("Query failed to run by returning code of {}.".format(response.status_code))

query = """{
    user(username: "magbanum"){
        username
        name
        tagline
        numFollowers
        publicationDomain
    }
}"""

result = run_query(query,headers)
print(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now let's run the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; python hashnode-user.py
{'data': {'user': {'username': 'magbanum', 'name': 'Shantanu Nighot', 'tagline': 'Electrical engineer | Self-taught developer | Hashnode blogger', 'numFollowers': 2, 'publicationDomain': 'magbanum.tech'}}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And look at that, we have just used Hashnode API to collect the information from the Hashnode profile. You can see that the data is in form of a dictionary and what if we don't want the whole dictionary but the part of it. We can do that too. This &lt;code&gt;result&lt;/code&gt; dictionary works the same as the Python dictionary so we will be able to index using its keys.&lt;/p&gt;

&lt;p&gt;For example, if we want to get the tagline of a user, we just need to index the "result" dictionary as &lt;code&gt;result["data"]["user"]["tagline"]&lt;/code&gt;. Replace the last line in the code with the following,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(result["data"]["user"]["tagline"])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See the result, you will get your own tagline if you replace "magbanum" with your username.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; python hashnode-user.py
Electrical engineer | Self-taught developer | Hashnode blogger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This look really clean and personalized. Try printing out bunch of other things like &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;publicationDomain&lt;/code&gt;, and &lt;code&gt;numFollowers&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We have successfully created a query in GraphQL, used it in our Python program, created the function to run the query and got the expected results. This concludes the tutorial, and I hope you were able to use Hashnode APIs in your python programs.&lt;/p&gt;

&lt;p&gt;As a next step, you can add user Input to get the username for more personalized results. Visit the &lt;a href="https://github.com/magbanum/python-codes/blob/main/hashnode-user.py" rel="noopener noreferrer"&gt;Github repo&lt;/a&gt; for this code.&lt;/p&gt;

&lt;p&gt;I hope the above information was helpful to you. Please comment if you are facing trouble with the above code, I will try my best to fix it. And follow me on Hashnode for more such tutorials.&lt;/p&gt;

</description>
      <category>hashnode</category>
      <category>api</category>
      <category>graphql</category>
      <category>python</category>
    </item>
    <item>
      <title>How to hide Django SECRET_KEY on Public Repositories.</title>
      <dc:creator>Shantanu Nighot</dc:creator>
      <pubDate>Sat, 08 May 2021 13:57:55 +0000</pubDate>
      <link>https://dev.to/magbanum/how-to-hide-django-secretkey-on-public-repositories-22k8</link>
      <guid>https://dev.to/magbanum/how-to-hide-django-secretkey-on-public-repositories-22k8</guid>
      <description>&lt;p&gt;While working on the Django project you may have seen variables like SECRET_KEY and other DATABASE-related information which is considered sensitive. When uploading the source code of deployed Django project on the internet, these data should be properly managed to avoid any misuse. But removing them from repo every time you push your data can become a hectic job and can cause errors in production.&lt;/p&gt;

&lt;p&gt;Let's consider my Django project named " &lt;a href="https://quotes-gen-project.herokuapp.com/"&gt;quotes-gen-project&lt;/a&gt; " deployed on Heroku. It shows the random quote from the database with its author name and also allows visitors to add more quotes. This is my first Django project so I kept it as simple as possible.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
  &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Bs9fHo5v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1620478049620/prlbhzLq4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Bs9fHo5v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1620478049620/prlbhzLq4.png" alt="quotes-gen-project"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;You can visit the &lt;a href="https://github.com/magbanum/quotes-gen-project"&gt;Github repo&lt;/a&gt; for the source code of this project.&lt;/p&gt;

&lt;p&gt;After Deploying the project on Heroku, you will have the following variables in the settings.py file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SECRET_KEY = 'your-django-secret-key'

DATABASES = {
      'default': {
        'ENGINE': 'your-database-engine name',
        'NAME': 'database-name',
        'USER': 'database-username',
        'PASSWORD': 'database-password',
        'HOST': 'database-host'
        'PORT': '5432',
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now follow the below steps to add the above environment variables in our Heroku config vars.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Login to your Heroku account&lt;/li&gt;
&lt;li&gt;Select your Heroku app&lt;/li&gt;
&lt;li&gt;Go to settings&lt;/li&gt;
&lt;li&gt;And click on &lt;code&gt;Reveal Config vars&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;
  &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gx847IqJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1620479327803/WpVHZ5vD3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gx847IqJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1620479327803/WpVHZ5vD3.png" alt="Heroku-settings"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Here you need to add the key and value for the variables you want to add. For example, add &lt;code&gt;SECRET_KEY&lt;/code&gt; in key and &lt;code&gt;your-django-secret-key&lt;/code&gt; in value without quotes. Do this for all other variables like NAME, USER, PASSWORD, and HOST.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
  &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1geksxi6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1620479371517/PZYvWOXZ5_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1geksxi6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1620479371517/PZYvWOXZ5_.png" alt="Heroku-config-vars"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;You have now added config vars in the app and need to do some changes to address them from our code.&lt;/p&gt;

&lt;p&gt;Go to the settings.py file and do the following changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SECRET_KEY = os.getenv('SECRET_KEY')

DATABASES = {
      'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv('NAME'),
        'USER': os.getenv('USER'),
        'PASSWORD': os.getenv('PASSWORD'),
        'HOST': os.getenv('HOST'),
        'PORT': '5432',
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it.&lt;/p&gt;

&lt;p&gt;Now run the following commands in the command prompt in your project root directory and check that everything is working as before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add -A
git commit -m "commit messege"
git push heroku master
heroku open
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hurray, you were able to hide sensitive data from source code and can now share your work with the world without any worries.&lt;/p&gt;

&lt;p&gt;Thanks for checking out this blog. I hope this information was helpful for you. Let me know by commenting below.&lt;/p&gt;

</description>
      <category>django</category>
      <category>heroku</category>
      <category>github</category>
      <category>python</category>
    </item>
  </channel>
</rss>
