<?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: Manas Paul</title>
    <description>The latest articles on DEV Community by Manas Paul (@imanaspaul).</description>
    <link>https://dev.to/imanaspaul</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%2F188032%2Fba2f0ac6-bb7f-4dd2-80f1-4507528a6a13.jpeg</url>
      <title>DEV Community: Manas Paul</title>
      <link>https://dev.to/imanaspaul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imanaspaul"/>
    <language>en</language>
    <item>
      <title>How to deploy your django app to heroku</title>
      <dc:creator>Manas Paul</dc:creator>
      <pubDate>Sun, 18 Jul 2021 15:56:10 +0000</pubDate>
      <link>https://dev.to/imanaspaul/how-to-deploy-your-django-app-to-heroku-1k51</link>
      <guid>https://dev.to/imanaspaul/how-to-deploy-your-django-app-to-heroku-1k51</guid>
      <description>&lt;p&gt;How to make web applications with the help of django web framework, I have already few tutorials of that and  in the whole web you could find a lot of tutorials. But when it comes to deploy the web application that you have build, it's really frustrating. I have found some outdated tutorials on Google about how to deploy a Django web application to heroku. So I though why not make a updated post about it. &lt;/p&gt;

&lt;p&gt;So in this tutorial we are going to cover how you can deploy a django appliction to heroku web services.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you will going to learn
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;1. Make simple application in django&lt;/li&gt;
&lt;li&gt;2. Prepare the application for Heroku&lt;/li&gt;
&lt;li&gt;3. Use git for verision control&lt;/li&gt;
&lt;li&gt;4. Deploy to heroku&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  prerequisite
&lt;/h2&gt;

&lt;p&gt;Before you get started with this tutorial you have have these knowledges.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have to know the basics of Django&lt;/li&gt;
&lt;li&gt;You have to have some knowledge about Git&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Make simple application in django
&lt;/h3&gt;

&lt;p&gt;I know all of you're so smart so, if you already have the application ready you can &lt;code&gt;skip&lt;/code&gt; this step. For this tutorial I am going to use &lt;code&gt;pipenv&lt;/code&gt; for virtual enviorment. You could use anything for that. So the first step is to create the virtual enviorment and install the dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pipenv &lt;span class="nb"&gt;install &lt;/span&gt;django

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

&lt;/div&gt;



&lt;p&gt;after installing that we need to activate the virtual enviorment, so to do that run this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pipenv shell
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now everything is ready to go, you can now create your django project inside the virtual enviorment. To do that you can run this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;django-admin startproject main &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case I'm calling my project &lt;code&gt;main&lt;/code&gt; and using a dot to avoud creating another folder called &lt;code&gt;main&lt;/code&gt; to create the project. After you done all of that, now we have finished out first part.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Prepare the application for Heroku
&lt;/h3&gt;

&lt;p&gt;In this step we need to prepare our application for production. So first thing is to open the &lt;code&gt;settings.py&lt;/code&gt; file and do this changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt; &lt;span class="c1"&gt;# at the top
&lt;/span&gt;
&lt;span class="n"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SECRET_KEY"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"teyd23e2efvdgsf3dv2d362"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# SECURITY WARNING: don't run with debug turned on in production!
&lt;/span&gt;&lt;span class="n"&gt;DEBUG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'DEBUG'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;ALLOWED_HOSTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'*'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;STATIC_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'/static/'&lt;/span&gt;
&lt;span class="n"&gt;MEDIA_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'/media/'&lt;/span&gt;
&lt;span class="n"&gt;STATICFILES_DIRS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE_DIR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'staticfiles'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="n"&gt;STATIC_ROOT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE_DIR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'static'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;MEDIA_ROOT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE_DIR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'media_root'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this changes, we are getting our secret key and debug mode from the enviorment variables and adding some static and media urls. Now we need to install some dependency to work with heroku. So just install these dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pipenv &lt;span class="nb"&gt;install &lt;/span&gt;dj-database-url gunicorn psycopg2 whitenoise
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After install all of these dependencies, now go back to your &lt;code&gt;settings.py&lt;/code&gt; file and add some more settings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;dj_database_url&lt;/span&gt; 
&lt;span class="n"&gt;MIDDLEWARE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;'whitenoise.middleware.WhiteNoiseMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# add this new middleware
&lt;/span&gt;    &lt;span class="s"&gt;'django.middleware.security.SecurityMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.sessions.middleware.SessionMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.middleware.common.CommonMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.middleware.csrf.CsrfViewMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.auth.middleware.AuthenticationMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.contrib.messages.middleware.MessageMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'django.middleware.clickjacking.XFrameOptionsMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;STATICFILES_STORAGE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"whitenoise.storage.CompressedManifestStaticFilesStorage"&lt;/span&gt;


&lt;span class="n"&gt;prod_db&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;dj_database_url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn_max_age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;DATABASES&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'default'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prod_db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this changes we are using &lt;code&gt;whitenoise&lt;/code&gt; to managing out static files and &lt;code&gt;dj_database_url&lt;/code&gt; to connecting with production database. After you done all that, now we need to create a new file called &lt;code&gt;Procfile&lt;/code&gt; and paste this lines 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;web: gunicorn main.wsgi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this file we are using web processes and telling heroku to run the command called &lt;code&gt;gunicorn main.wsgi&lt;/code&gt;. Remember put your project name instead of mine eg. &lt;code&gt;web: gunicorn &amp;lt;your project name&amp;gt;.wsgi&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In case of using pipenv, I don't need to create a &lt;code&gt;requirements.txt&lt;/code&gt; file, Heroku will install all the dependencies from &lt;code&gt;Pipfile&lt;/code&gt;. But if you're using virtual env then &lt;em&gt;Don't forget to create the &lt;code&gt;requirements.txt&lt;/code&gt; file&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Use git for verision control
&lt;/h3&gt;

&lt;p&gt;If you alredy familiar with &lt;code&gt;git&lt;/code&gt; you alredy know all of these command. to commit all of the changes you have done write these command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Initial commit"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you done all of that, now it's time for the magial step to deploy the &lt;code&gt;django application to Heroku&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Deploy to heroku
&lt;/h3&gt;

&lt;p&gt;To deploy to heroku you need to have an account first. if you don't have any account yet, you can create the account, it's pretty simple and straight forward. After you create that now you need to have Heroku CLI. If you don't have it, you can &lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;Download it from here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you have all of them, now go to your command line and write this command to create a heroku porject.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;heroku create &amp;lt;write your project name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Sometime heroku gives error if the name alredy exists to better if you don't pass any name, heroku will create a reandom name for your project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once everting is setup properly, now it's time to push your code to &lt;code&gt;Heroku&lt;/code&gt; to see your website live. To do that write this command in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you done everthing correctly you can have your website live, in case if you're facing any issue you can comment down below, I will help you as much as possible. &lt;br&gt;
Thanks for reading through the tutorial. If you find this article helpful please leave a comment below or share it with your friends.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sometime heroku gives error if the name alredy exists to better if you don't pass any name, heroku will create a reandom name for your project.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once everting is setup properly, now it's time to push your code to Heroku to see your website live. To do that write this command in your terminal.&lt;/p&gt;

&lt;p&gt;git push heroku master&lt;/p&gt;

&lt;p&gt;If you done everthing correctly you can have your website live, in case if you're facing any issue you can comment down below, I will help you as much as possible. Thanks for reading through the tutorial. If you find this article helpful please leave a comment below or share it with your friends.&lt;/p&gt;

&lt;p&gt;For more tutorial visit our blog manascode.com&lt;/p&gt;

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