<?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: Akinwande Tomisin</title>
    <description>The latest articles on DEV Community by Akinwande Tomisin (@tomjames156).</description>
    <link>https://dev.to/tomjames156</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%2F1028768%2F54ca4630-2b60-4f14-bf6c-c2342074105b.jpeg</url>
      <title>DEV Community: Akinwande Tomisin</title>
      <link>https://dev.to/tomjames156</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tomjames156"/>
    <language>en</language>
    <item>
      <title>HOW TO HOST A DJANGO PROJECT ON RAILWAY</title>
      <dc:creator>Akinwande Tomisin</dc:creator>
      <pubDate>Sun, 09 Apr 2023 21:31:09 +0000</pubDate>
      <link>https://dev.to/tomjames156/how-to-host-a-django-project-on-railway-3f09</link>
      <guid>https://dev.to/tomjames156/how-to-host-a-django-project-on-railway-3f09</guid>
      <description>&lt;p&gt;Hey everyone 👋🏾. This article is going to show the updated way to host a Django application on Railway. Let's get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  Brief Introduction
&lt;/h3&gt;

&lt;p&gt;Railway is a cloud hosting platform where you can host your projects and provision infrastructure like databases. It's popularly known as an alternative to Heroku. &lt;/p&gt;

&lt;h2&gt;
  
  
  Tutorial
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: To get started, you'll need to have a &lt;a href="https://github.com/" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; account containing a repository of the project you want to be hosted. You'll also need to create a Railway account if you don't have one already. Click &lt;a href="https://railway.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to Sign up for Railway.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: In your project, run the command:&lt;/p&gt;

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

pip install gunicorn


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Run the command below to get all the packages used in your project:&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;&lt;strong&gt;Step 4&lt;/strong&gt;: Create a file called &lt;code&gt;railway.json&lt;/code&gt; in your projects root folder. Copy &amp;amp; Paste the following code into the file.&lt;/p&gt;

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

{
    "$schema": "https://railway.app/railway.schema.json",
    "build": {
        "builder": "NIXPACKS"
    },
    "deploy": {
        "startCommand": "python manage.py migrate &amp;amp;&amp;amp; python manage.py collectstatic --noinput &amp;amp;&amp;amp; gunicorn your_app_name.wsgi",
        "restartPolicyType": "ON_FAILURE",
        "restartPolicyMaxRetries": 10
    }
}


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

&lt;/div&gt;

&lt;p&gt;Replace the string 'your_app_name' in "startCommand" with the name of your Django project i.e The folder that contains a &lt;code&gt;wsgi.py&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Next, you'll make some changes to the &lt;code&gt;settings.py&lt;/code&gt; file. Check for the line &lt;code&gt;ALLOWED HOSTS = []&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Change the line of code to: &lt;code&gt;ALLOWED HOSTS = ['*']&lt;/code&gt; to allow all hosts for the mean time.&lt;/p&gt;

&lt;p&gt;Still on your &lt;code&gt;settings.py&lt;/code&gt; file, navigate to the static files section and add the following code:&lt;/p&gt;

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

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Next, run the following command to collect all your static files into a folder.&lt;/p&gt;

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

python manage.py collectstatic


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: You can now push the code to a repository on GitHub. Run the following commands consecutively to do that:&lt;/p&gt;

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

git status
git add .
git commit -m "initial commit"
git push origin main


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 7&lt;/strong&gt;: Log in to your Railway app Account&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select New Project
&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%2Fq82tr04y267hfoudo429.png" alt="New Project"&gt;
&lt;/li&gt;
&lt;li&gt;Click on 'Deploy from GitHub Repo' 
&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%2F4udp4dx403lvy9gdqys7.png" alt="Deploy from GitHub Repo"&gt;
&lt;/li&gt;
&lt;li&gt;Click on 'Configure GitHub App'.
&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%2F7bmkvyjguftucuqs231g.png" alt="Configure GitHub App"&gt;
&lt;/li&gt;
&lt;li&gt;Log in to your GitHub Account in the Dialog window. Navigate to the Repository Access section and select the repository you would like to deploy.&lt;/li&gt;
&lt;li&gt;When you are done and redirected to the Railway App page, select 'Deploy from GitHub Repo' and choose the repository you want to deploy. The deployment should start immediately.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 7: Once the deployment is complete, you'll need to make some modifications to the &lt;code&gt;settings.py&lt;/code&gt; file of your project. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ir8p8t1qjagqcicz4ir.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%2F6ir8p8t1qjagqcicz4ir.png" alt="Getting Domain Name "&gt;&lt;/a&gt;&lt;br&gt;
Go to the Settings section of your newly deployed app, Go down to the Domain section under Environment, click on 'Generate Domain' and copy the domain name of the app. In the &lt;code&gt;settings.py&lt;/code&gt; file replace the '&lt;em&gt;' in `ALLOWED_HOSTS = ['&lt;/em&gt;']` with the your app's domain name without the 'https://' like so:&lt;/p&gt;

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

ALLOWED_HOSTS = ['purple-field-production.up.railway.app']


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

&lt;/div&gt;

&lt;p&gt;Add the following code to your &lt;code&gt;settings.py&lt;/code&gt; and replace the value of the variable with your app's full URL.&lt;/p&gt;

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

CSRF_TRUSTED_ORIGINS = ['https:purple-field-production.up.railway.app']


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

&lt;/div&gt;

&lt;p&gt;Finally, commit the changes made to your repository using the commands below and Railway will automatically redeploy the site with your changes.&lt;/p&gt;

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

git status
git commit -am "updated settings.py"
git push origin main


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

&lt;/div&gt;

&lt;p&gt;CONGRATULATIONS!!, your project has been deployed👍🏾&lt;/p&gt;

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