How to Deploy a Django App and Postgres Database to Render

Pretty Printed
29 Jun 202320:14

Summary

TLDRThis video provides a step-by-step guide on how to deploy a Django app along with a PostgreSQL database to the Render platform. The presenter explains how to set up an account, connect GitHub, and create a PostgreSQL database on Render. Key steps include configuring environment variables, adjusting settings in Django, handling deployment errors, and connecting the Django app to the database. The video also touches on debugging, deploying with custom domains, and troubleshooting common deployment issues. It's designed to demonstrate an easy and practical approach to deploying a Django project.

Takeaways

  • πŸš€ Render is an easy-to-use service for deploying web applications and databases.
  • πŸ”— The first step in deploying on Render is connecting your GitHub account to easily deploy code from a repository.
  • 🐍 In the example project, a simple Django app is deployed, demonstrating how to add 'Members' to a PostgreSQL database.
  • πŸ’Ύ Instead of using SQLite, the video shows how to switch to PostgreSQL by creating a database on Render and connecting it to the Django app.
  • πŸ”§ The database connection settings are managed using the 'dj-database-url' package in Django to parse the database URL.
  • πŸ’» The migration process requires installing PostgreSQL's 'psycopg2' package to create tables in the new database.
  • πŸ“Š The video demonstrates how to view and manage the database using a tool called DBeaver.
  • πŸ”‘ Several settings, including 'debug', 'allowed hosts', and 'secret key', are moved to environment variables for better security and flexibility.
  • 🌐 To deploy the Django app on Render, the video explains how to add a start command using 'Gunicorn' to run the app’s WSGI file.
  • πŸ› The video highlights common deployment issues, such as mismatched Django versions and host configuration problems, and how to troubleshoot them.

Q & A

  • What is Render, and how is it used in the video?

    -Render is a cloud platform used to deploy various services such as web apps, static sites, databases, and background workers. In the video, Render is used to deploy a Django web app and a PostgreSQL database.

  • How does the presenter connect the Django app to a PostgreSQL database?

    -The presenter creates a PostgreSQL database on Render, copies the external connection URL, and uses the 'dj-database-url' package to configure the Django app to connect to this database. The connection details are then added to the Django app's settings.

  • What are the main steps to deploy a Django app on Render?

    -The main steps include creating a Render account, connecting a GitHub repository with the Django app, configuring environment variables like debug, allowed hosts, and secret key, setting up a PostgreSQL database, and then deploying the app through Render's dashboard.

  • How does the presenter handle the database migration to the new PostgreSQL database?

    -The presenter first updates the database settings to connect to the PostgreSQL database, then runs `python manage.py migrate` to apply the migrations, creating the necessary tables in the new database.

  • Why does the presenter use environment variables for configuration in the Django settings?

    -The presenter uses environment variables to keep sensitive information, like the secret key and database URL, out of the codebase and the repository. This is a common practice for security and flexibility, allowing different configurations for different environments.

  • What issues does the presenter encounter during the deployment, and how are they resolved?

    -The presenter faces issues like incorrect Django version compatibility and an invalid allowed hosts setting. These are resolved by adjusting the Django version in the requirements file and correcting the allowed hosts environment variable.

  • Why does the presenter switch from using SQLite to PostgreSQL?

    -The presenter switches from SQLite to PostgreSQL because PostgreSQL is a more robust and scalable database suitable for production, whereas SQLite is generally used for development purposes.

  • How is the Django app's secret key managed in the deployment process?

    -The Django app's secret key is managed using an environment variable in Render. The key is generated and stored as an environment variable, ensuring it is not hardcoded in the source code.

  • What command is used to install the required Python packages during deployment?

    -The command `pip install -r requirements.txt` is used to install the required Python packages during deployment. This ensures that all the necessary dependencies, including Django and PostgreSQL drivers, are installed.

  • How does the presenter debug issues with the app after deployment?

    -The presenter temporarily enables debug mode by setting the debug environment variable to 'true'. This provides more detailed error messages, allowing them to identify and fix the issues, such as incorrect allowed hosts.

Outlines

00:00

πŸš€ Deploying a Django App on Render with PostgreSQL Integration

This paragraph introduces the video, where the creator demonstrates deploying a Django app on Render, along with setting up a PostgreSQL database. The host explains how Render's dashboard allows easy deployment of web services, databases, and more. After connecting their GitHub account to Render, they showcase a simple Django app that manages 'Members' in a database. The app currently uses an SQLite database, but the goal is to switch to PostgreSQL, showing the steps required to connect the new database in Render.

05:01

πŸ”— Setting Up and Connecting PostgreSQL Database

The focus here is on setting up the PostgreSQL database on Render. The user creates the database through Render's dashboard, specifies the region and version, and uses the free plan. They explain the difference between internal and external database connection URLs. The external connection URL is copied to the code, and the host demonstrates how to replace SQLite settings with PostgreSQL by using the 'dj-database-url' package for easier configuration. They run migrations and successfully connect the app to the PostgreSQL database, verifying this through a tool called DBeaver.

10:02

βš™οΈ Working with Database and Running the Django App Locally

The host uses DBeaver to view the tables generated by the Django app in the PostgreSQL database and verifies that no data exists yet. They then start the app locally, adding members ('Anthony,' 'Herbert,' and 'Pretty Printed') to the database. Afterward, they refresh DBeaver to confirm that the data has been successfully added. The section highlights that the app is connected to Render’s database. The creator also mentions future plans to use the internal database URL for deployment on Render.

15:03

πŸ’» Preparing for Deployment and Environment Variable Setup

In this section, the creator begins setting up the Django app for deployment by initializing a Git repository and discussing the importance of environment variables. They explain how to set debug mode, allowed hosts, and secret keys as environment variables in the `settings.py` file. They also cover configuring the database connection to read from environment variables. The host saves all changes to GitHub, demonstrating the need for a `requirements.txt` file to specify Python dependencies for Render to install during deployment.

20:04

πŸ“‚ Finalizing GitHub Setup and Deploying the App on Render

Here, the host finalizes the GitHub setup, pushing the code and dependencies (in `requirements.txt`) to a new repository. They explain the steps to link the GitHub repo to Render, specifying the branch, runtime (Python 3), and build commands. The host installs `Gunicorn` to serve the Django app and configures the deployment settings in Render’s dashboard. They also discuss choosing the free plan for hosting and anticipating possible deployment issues, emphasizing that deployment is often iterative with adjustments required.

🐞 Debugging Common Deployment Issues

The deployment faces version compatibility issues with Django. The creator explains how to resolve these by downgrading Django to version 3.2.19. They highlight the importance of debugging deployment problems and adjusting configurations (like Django version) as needed. The step-by-step approach to resolving issues helps viewers understand the challenges and solutions during the deployment process.

πŸ”§ Environment Variables and Fixing Errors

The deployment progresses, but the app runs into errors related to missing environment variables (secret key, allowed hosts, and debug mode). The host demonstrates adding these environment variables via Render’s dashboard and provides a detailed explanation of each setting. After adding the internal database URL, they show how to trigger a redeployment. They also demonstrate enabling Django’s debug mode temporarily to identify specific errors, such as an incorrect allowed hosts configuration, and explain how to fix these issues.

🌐 Final Testing and Custom Domain Setup

The app is finally live after troubleshooting the deployment issues. The host verifies the app by adding new members and checking that the data is saved correctly. They also explain how to disable debug mode once everything is working. The paragraph ends with a brief introduction to setting up a custom domain for the app on Render, though this step isn’t demonstrated fully in the video.

πŸ‘ Conclusion and Final Remarks

The host wraps up the video by thanking viewers and encouraging them to leave comments if they have questions. They highlight that the video focused on deploying a Django app to Render and resolving common deployment issues, including working with PostgreSQL. They also encourage viewers to subscribe for more content.

Mindmap

Keywords

πŸ’‘Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. In the context of the video, Django is used to build a web application that the creator wants to deploy. The script mentions setting up a simple Django app to demonstrate the deployment process, indicating its role as the core application being showcased.

πŸ’‘Render

Render is a cloud platform that offers a variety of services for deploying web applications, including static sites, web services, background workers, and databases. The video's theme revolves around deploying a Django app to Render, highlighting its ease of use and the services it provides. Render is central to the tutorial as the platform where the Django app and PostgreSQL database are deployed.

πŸ’‘PostgreSQL

PostgreSQL, often referred to as Postgres, is a powerful, open-source object-relational database system. In the video script, the creator decides to switch from using a SQLite database to a PostgreSQL database for their Django app. PostgreSQL is highlighted as a scalable and robust solution for database needs, which is essential for the app's backend.

πŸ’‘Environment Variables

Environment variables are dynamic-named values that can affect the way running processes will behave on a computer. In the video, the creator sets up environment variables for the Django app on Render, such as the secret key, allowed hosts, and database URL. These variables are crucial for configuring the app's settings without hardcoding sensitive information, ensuring security and flexibility.

πŸ’‘GitHub

GitHub is a web-based platform for version control and collaboration, allowing developers to work together on projects. The video script describes connecting a GitHub account to Render for easy deployment of the Django app. GitHub serves as the repository where the app's code is stored and managed, emphasizing the importance of version control in the deployment process.

πŸ’‘Database Migration

Database migration refers to the process of moving data from one database system to another or changing the structure of the database. In the script, the creator performs a database migration by running 'python manage.py migrate' after switching to a PostgreSQL database. This step is essential for creating the necessary database tables for the Django app to function correctly.

πŸ’‘WSGI

WSGI stands for Web Server Gateway Interface, a standard interface between a web server and web applications or frameworks written in Python. The video mentions using Gunicorn, a WSGI HTTP server, to serve the Django app. WSGI is integral to the deployment process as it defines how the web server communicates with the Django application, enabling it to handle HTTP requests and responses.

πŸ’‘Requirements.txt

A 'requirements.txt' file is a common way to specify project dependencies for Python applications. It lists all the packages that need to be installed for the project to run. In the video, the creator generates a 'requirements.txt' file using 'pip freeze' to ensure that Render can install the necessary libraries to deploy the Django app. This file is crucial for maintaining consistency in the app's environment across different stages of development and deployment.

πŸ’‘Deployment

Deployment is the process of making a software application or service available to users. The main theme of the video is deploying a Django app to Render, which includes setting up the database, configuring environment variables, and pushing the code to GitHub. The deployment process is detailed step by step, showing viewers how to overcome common issues and ensure a successful deployment.

πŸ’‘Secret Key

In Django, the secret key is a unique identifier used to provide cryptographic signing. It's crucial for security and should be kept secret. In the video, the creator sets a secret key as an environment variable on Render to ensure it's not hardcoded into the app's code. This practice is important for maintaining the security of the Django app by preventing theζ³„ιœ² of sensitive information.

πŸ’‘Debug

Debug mode in Django is a setting that provides detailed error pages and other debugging information. In the video, the creator sets the DEBUG environment variable to control whether the Django app runs in debug mode. Initially, it's set to true for debugging purposes, but it's later changed to false for the live deployment, illustrating the importance of disabling debug mode in a production environment to prevent the exposure of sensitive information.

Highlights

Introduction to deploying a Django app to Render and connecting it with a PostgreSQL database.

Render is an easy-to-use service for deploying web services, static sites, cron jobs, PostgreSQL, and more.

Connecting GitHub to Render allows for seamless deployment of Django apps.

Demonstrating how to switch the database from SQLite to PostgreSQL by setting up a PostgreSQL database on Render.

Using the Django database URL library for easy configuration of database settings in settings.py.

Installing necessary PostgreSQL drivers (psycopg2) to connect Django to the PostgreSQL database.

Verifying the PostgreSQL connection using DBeaver, a tool to interact with databases.

Successfully connecting Django app to PostgreSQL by adding data through the app and seeing the updates in the PostgreSQL database.

Steps to deploy a Django app on Render by connecting the GitHub repository and setting up environment variables for secret key, debug, and allowed hosts.

Using environment variables to secure sensitive data like the secret key and configuring allowed hosts in Django.

Handling common deployment issues, such as incorrect Django versions, by troubleshooting and downgrading to a compatible version.

Setting up the Gunicorn server to run Django's WSGI application on Render for production deployment.

Debugging errors related to allowed hosts by updating environment variables during the deployment process.

Adding new members through the deployed app and verifying that the data is successfully saved in the connected PostgreSQL database.

Instructions for setting a custom domain for the deployed Django app on Render, improving its URL accessibility.

Transcripts

play00:00

hey everyone in today's video I'm going

play00:02

to show you how to deploy a Django app

play00:04

to a service called render and along

play00:06

with a Django app I will deploy a

play00:08

database as well so you can see how to

play00:10

connect this postgresql database that I

play00:12

create to the Django app and then the

play00:15

Django app will be running on render so

play00:18

render is a service that is very easy to

play00:20

use it's very easy to deploy the things

play00:22

that they have here so I'm on their

play00:24

dashboard and you see you can deploy

play00:26

static sites web services so Django

play00:29

would be a web service background

play00:30

workers cron jobs postgresql and other

play00:33

things so the main thing you need to do

play00:35

after creating an account is to connect

play00:37

your GitHub account so here my GitHub

play00:40

login has been connected pretty printed

play00:43

and that will allow me to deploy the

play00:46

Django app easily so the app I'm going

play00:48

to deploy is this which is very simple

play00:50

this is just for demonstration purposes

play00:53

so this is a jingle app and it just

play00:55

allows me to add something I call

play00:56

Members so I'll just put my last name

play00:58

here Herbert and we see I've added

play01:00

Anthony pretty printed in Herbert to the

play01:02

database and the app looks like this so

play01:05

I have this render deploy app I have

play01:07

this example app within the Django

play01:10

project and in the views.pi I simply a

play01:14

query for all the members and then I add

play01:16

a member here by creating it and then

play01:18

just redirecting to the index so the app

play01:20

itself is very simple the reason why I

play01:22

made it this simple is so I can

play01:24

demonstrate the deployment process not

play01:26

so much building an app so if I go over

play01:28

here to settings.pi I can go down to the

play01:30

database part and this is the thing that

play01:32

I want to do first so right now I'm

play01:34

using this sqli database and it's

play01:36

generating the file here what I want to

play01:38

do is I want to use a postgresql

play01:40

database instead so I'll start on render

play01:43

by creating a postgresql database and

play01:45

connecting to it here on my local

play01:47

machine so let me go over to the render

play01:49

dashboard and I'll click new postgresql

play01:52

and then I'll just give it a name so

play01:55

uh let's call this pretty printed Django

play01:58

render

play01:59

and then I'll leave the database name to

play02:01

be randomly generated along with the

play02:03

user because I'm not too concerned about

play02:05

those I'll leave the region as origin

play02:07

the version is 15 that's the latest I'm

play02:10

not using datadog and I'm using the free

play02:12

plan here so I'll just click create

play02:14

database

play02:15

and in a moment it will create it so

play02:18

I'll wait for this to be done so now we

play02:20

see the status is changed to available

play02:22

the storage status is still pending but

play02:24

while we wait for that I can go to the

play02:26

connect information here and explain it

play02:28

so here I have two tabs one fit internal

play02:31

connection and one for external

play02:33

connection so if I'm connecting to the

play02:35

database from outside of render I want

play02:37

to use this external connection URL but

play02:40

if my app is hosted on render as a web

play02:42

service then I want to use the internal

play02:44

database URL so I'm going to copy the

play02:46

external database URL just copy that and

play02:49

we see the storage is ready now so I

play02:50

have one gigabyte of storage now go over

play02:53

to my code and inside of the code I'll

play02:56

just paste this as a comment so there

play02:58

are a couple ways you can do this you

play02:59

can set up everything here so you can

play03:02

change the engine to be postgresql you

play03:04

can change the name to be the database

play03:06

name and then you can put the username

play03:07

and password but I like to use a Django

play03:10

database URL so I will install that so

play03:14

pip install DJ J database URL and then

play03:18

I'll just go ahead and import that at

play03:20

the top so import DJ database URL and

play03:24

then I'll just use it down here so what

play03:27

I'm going to do is I'm going to

play03:28

overwrite the existing database settings

play03:32

output default here

play03:35

and I'll use that Django database

play03:37

url.parse and then I can pass in my

play03:41

URL here

play03:43

so I'll just cut and paste that into

play03:47

that okay so I'll remove this comment

play03:52

and I'll save this and now that I have a

play03:54

separate database what I'll do is I'll

play03:56

do python

play03:58

manage.pi migrate because I need to

play04:01

create the

play04:03

tables in the new database and we see I

play04:05

have an error here and this is simply

play04:07

because I don't have the postgres driver

play04:09

installed so I'll install that on my

play04:11

system it's cycle pg2 binary but on

play04:14

yours you might be able to get away with

play04:17

um cycle pg2

play04:19

without the binary so I'll go ahead and

play04:21

install that and now I'll run migrate

play04:23

again

play04:25

and this time is running and it's not

play04:27

giving us any errors so while that runs

play04:30

I'm going to connect to my database

play04:33

directly with a tool called D Beaver so

play04:35

let me open that up and then what I can

play04:37

do is I can connect to it so this is

play04:38

just a tool to work with databases so

play04:41

I'll use postgresql as my database and

play04:44

then I can copy the URL in here and just

play04:47

take out the things that go into this

play04:49

certain field so that was the database

play04:51

name so I'll put it here pretty printed

play04:53

Django render the host is going to be

play04:56

everything after the AD Sign

play04:58

the username is going to be this user

play05:01

here so let me go ahead and take out the

play05:04

user

play05:05

so

play05:07

I can just grab after the r

play05:11

and then paste that into username

play05:13

and then after the colon but up into the

play05:16

at sign that's going to be the password

play05:17

and then that should be it so everything

play05:20

after the ads is the host I can test

play05:23

connection it looks like it's working so

play05:26

finish it appears here on the left so

play05:28

now I can open it and when I do open it

play05:31

I should be able to see the tables that

play05:33

were generated from my app so I can open

play05:36

up databases here schema public tables

play05:38

and then these are the tables and I have

play05:40

the example member table here so let's

play05:43

look at that

play05:44

because right now

play05:46

there should be no data in it so there's

play05:48

no data but now if I go to the running

play05:51

app so let me go ahead and start the app

play05:54

first so

play05:56

python managed up high run server

play06:01

and then I go over here when I reload

play06:04

the page we see there are no members so

play06:06

I'll go ahead and add Anthony as a

play06:09

member

play06:12

I'll add Herbert as a member

play06:18

and I'll add pretty

play06:21

printed as a member

play06:25

okay great so I added those three I can

play06:27

look at the database again and I'll just

play06:29

refresh this here and we see it has

play06:32

three entries Anthony Herbert and pretty

play06:35

print it in there so we know we are now

play06:36

connected to the render database and

play06:39

everything is working properly so as you

play06:41

can see it was very easy to set up the

play06:42

database and connect to it

play06:45

so later I'm going to come back and use

play06:46

this internal connection so I can

play06:48

connect to the database using this

play06:50

internal URL and the process is going to

play06:52

be pretty much the same I'm just

play06:54

switching out that URL so now let's

play06:56

think about deploying the Django app so

play06:58

I'll go back to the dashboard

play07:00

and I'll click on new here and I want to

play07:03

create a new web service

play07:05

so what it wants me to do here is to

play07:07

connect a repo that I have but I haven't

play07:10

created a repo for my project yet so

play07:12

let's go ahead and do that first so I

play07:14

can go here and I can do get a knit

play07:17

and I've turned this into a repo and

play07:20

what I want to do here is I'll skip the

play07:24

ignoring files but what I want to do in

play07:26

the settings.pi is I want to set certain

play07:28

things up to come from environment

play07:29

variables because I don't want some

play07:31

information to go in the repo so let's

play07:34

go through it I'm going to import OS so

play07:36

I can grab environment variables so one

play07:39

thing that needs to be an environment

play07:41

variable is debug so right now it's just

play07:43

true but I want this to be the value

play07:47

from the environment variable so what I

play07:49

can do is I can say something like OS

play07:51

dot Environ

play07:53

baguettes and I'm going to get a key

play07:57

called debug so I'm going to name the

play07:59

environment variable debug when I go to

play08:01

create it

play08:02

and it's going to return false by

play08:05

default

play08:06

and I want to check to see if this is

play08:09

equal to true so debug takes in a

play08:12

Boolean so I'm going to see if the debug

play08:14

value itself is a string that's true so

play08:17

if it's true it's going to equal true

play08:19

and actually to make this even better I

play08:21

can lower this and then

play08:24

but it's a lowercase shoe so if I put

play08:26

upper cases or all lowercase in the

play08:29

environment variable this will still

play08:30

work

play08:31

so as long as the string in the

play08:34

environment variable debug is true this

play08:37

will equal true if it's anything else

play08:39

then it's going to be false allowed

play08:41

hosts I can also create an environment

play08:43

variable for I'll do OS Environ

play08:47

dot gets and I want to get

play08:51

allowed posts so I'll just call

play08:54

everything the same as the setting in

play08:55

Django and what I'm going to do is I'm

play08:57

going to split it

play08:59

by spaces so this should be an S you'll

play09:02

see me use this when I go to the render

play09:04

dashboard and the idea here is I can

play09:06

have multiple hosts separated by spaces

play09:08

and then it's going to split them and

play09:10

return a list as you saw before it was a

play09:13

list and then after it will be a list

play09:14

and then finally the secret key I can do

play09:17

something similar

play09:19

this one is just straightforward I can

play09:20

do OS and viron git

play09:23

Secret

play09:24

okay

play09:26

so those are the three things that I

play09:28

wanted to

play09:29

make environment variables up here and

play09:32

then the last thing will be the actual

play09:33

database so

play09:35

here I can say like database URL equals

play09:39

OS Environ gets database URL

play09:44

and then I will

play09:46

remove the string and replace it with

play09:49

the variable

play09:51

so this is a really straightforward way

play09:53

of setting up the settings.pi there are

play09:55

many ways of setting this up so don't

play09:57

think that this is the best way this is

play09:59

just the way I wanted to do it for this

play10:00

video's purpose but there are different

play10:02

approaches to having settings up high

play10:04

you can have multiple settings.pi you

play10:06

can use like the Django Environ Library

play10:08

you can do multiple things but for the

play10:10

purposes of this video I just wanted to

play10:12

keep it like this so those are the

play10:14

things that I need to make environment

play10:15

variables so now I'll just go ahead and

play10:17

save everything to the repo so get at

play10:19

all git commit

play10:22

first commits and I probably should have

play10:24

added a git ignore because I have my

play10:27

virtual environment here so what I can

play10:28

do is I can actually create the git

play10:31

ignore

play10:34

and then I can do git remove

play10:36

hashed

play10:39

and then the EnV directory

play10:46

Cash Dash R the EnV directory

play10:50

and then in the git ignore I can just do

play10:53

slash gits

play10:56

and then I can do git add all again get

play11:00

commits I removed EnV okay

play11:04

so now the EnV directory is no longer my

play11:06

git repos so it shouldn't take so long

play11:08

to commit next time there are other

play11:09

things that I could ignore like uh Pi

play11:11

cash and uh the sqlite file but I'm just

play11:15

going to leave that like this and the 10

play11:17

000 here is no longer correct because

play11:19

I've already committed the repo okay so

play11:21

now I want to go to GitHub and I want to

play11:23

create a repository so I'll call this

play11:25

render

play11:27

Django example

play11:29

and then I'll set this to private and

play11:31

I'll create the repo

play11:34

and it's going to give me the get remote

play11:36

ad origin link down here so I'll just

play11:38

copy that and I'll paste it in here so

play11:41

git remote ad origin and then I can do

play11:44

git push origin Master that's going to

play11:46

push all my code over to GitHub

play11:50

so that's done I can refresh now we see

play11:54

this here the EnV directory is gone

play11:56

because I removed it so now let's go and

play11:59

do one more thing because this is a

play12:01

python app I need a requirements.txt so

play12:03

I have the virtual environment working

play12:05

on my local machine but when render goes

play12:09

to deploy this it needs to install all

play12:10

the same libraries so what I can do

play12:13

as I can do uh pip freeze and then put

play12:17

this in requirements.txt

play12:19

and then I can just add that to my

play12:24

commits

play12:26

edit requirements and then just do get

play12:29

push origin Master again

play12:32

and now we see when I refresh

play12:35

the requirements.txt is here so anytime

play12:37

you update the requirements.txt you'll

play12:39

need to push it to your repo and then

play12:41

redeploy the service to render because

play12:43

it needs to install the new libraries so

play12:46

that's it for the GitHub site now let me

play12:48

just refresh this page so my new repo

play12:51

loads and when it does I can just find

play12:53

it and hit connect so there it is I'll

play12:55

hit connect and then I'll give it a name

play12:57

so Django render example app

play13:01

uh the region will be Oregon West's

play13:03

again the branch will be master so if

play13:05

you had a different branch name you can

play13:06

use that but in my case it's just Master

play13:08

the root directory is not relevant for

play13:11

this uh the runtime will be python3 so

play13:14

this is a python project and then the

play13:16

build command is going to be pip install

play13:18

requirements.txt so that's why I just

play13:20

generated the requirements.txt and now

play13:23

we see the Star Command which is going

play13:24

to actually run the app so I should

play13:26

install G unicorn

play13:29

and then I can do pip freeze

play13:32

requirements.txt again

play13:34

and then I can add this to the repo

play13:40

and just push it

play13:45

and the idea here is I can run this wsgi

play13:49

so what I can do is I can say G unicorn

play13:54

and then the name of my app so the name

play13:56

of my app is render deploy

play13:59

render deploy Dot wsgi and if there are

play14:03

any problems with this we'll see it when

play14:05

it goes to deploy

play14:07

so the instance type is free once again

play14:09

but of course you're going to have

play14:11

another one if you decide to pay

play14:14

and then I'll create the web service so

play14:16

that's all I need for right now

play14:19

and then it's going to try to deploy it

play14:21

so there are going to be problems with

play14:22

the deploy and I'll fix them as they

play14:25

come up to kind of show you how other

play14:27

things work here okay so the first error

play14:29

we get is a can't find Django 4.2.2 so

play14:32

at the time of recording this video

play14:33

that's the version of Django that I'm

play14:35

using because we see here in the

play14:37

requirements.txt it displays as 4.2.2 so

play14:41

the way to fix this is I can just

play14:43

downgrade so let me downgrade to four

play14:46

0.0 and once again I can just

play14:51

commit everything so because I modify

play14:53

requirements.txt directly I can just

play14:56

commit without having to do Pit freeze

play14:58

so I get commit and then change Django

play15:03

version

play15:04

so git push origin master so this is

play15:07

something you probably won't have to do

play15:08

but in my case the versions are just out

play15:11

of date for from wherever it's pulling

play15:13

the versions so I'll go back here

play15:17

and now I can do uh manual deploy and

play15:21

then deploy latest commit so I can start

play15:23

the process again and once again there

play15:25

are going to be problems but like I said

play15:26

we can deal with them as they come up

play15:28

and it looks like it doesn't even take

play15:29

4.0 so the latest version here is 3.2.19

play15:33

so I'll just change it again

play15:35

so the reason why I show these in the

play15:37

video these um

play15:39

failures is because oftentimes when

play15:42

people go to deploy apps it doesn't work

play15:44

exactly perfectly the first time around

play15:47

so I just want to go through the process

play15:49

of debugging everything so you can see

play15:50

what's happening so I updated Django

play15:54

version again so there are very little

play15:56

differences between Django 3 and Django

play15:59

4 that's why I can get away with doing

play16:00

something like this but but normally you

play16:02

want to keep the version as close to the

play16:03

latest as possible

play16:05

so I'll go back here and I'll once again

play16:07

uh do the manual deploy and let's see

play16:10

what happens this time because there are

play16:12

going to be more issues okay so we see

play16:14

that the build was successful and now

play16:16

it's deploying so I don't expect the

play16:18

deploy itself to work but we'll see what

play16:21

happens when it finishes and just keep

play16:23

in mind that when you're using the free

play16:24

plan this is going to be a bit slow

play16:26

because if you want the fastest you need

play16:28

to pay for it okay so here I see a loud

play16:31

host

play16:33

um is missing and the reason is is

play16:35

because I haven't specified that in my

play16:38

environment so what I can do is I can go

play16:40

over to the environment here

play16:42

and I want to specify all the

play16:45

environment variables that I have so I

play16:46

can add them so one will be secret key

play16:49

and I can just generate a secret key

play16:52

here

play16:53

right so it doesn't really matter what

play16:56

the secret key is it just needs to be

play16:57

secret

play16:58

I'll add another one this will be

play17:01

allowed hosts

play17:03

and the value for this one

play17:06

should be a string

play17:09

with spaces between the hosts so I'll

play17:12

just do that in localhost really you

play17:14

don't need localhost but just to

play17:16

demonstrate that this is going to be a

play17:18

string with two things I'll do it like

play17:19

that

play17:20

so I can add another one and I need the

play17:25

debug

play17:27

so I want the debug here to be false and

play17:30

I'll add one more for this one I want

play17:32

the database URL and now we can go back

play17:36

to our database so I'll just open a new

play17:38

tab here and I'll go over to the

play17:41

database that I have so uh the Django

play17:44

render database and I want to get the

play17:47

internal connection URL this time copy

play17:50

that and just paste that into the

play17:52

environment variable value so now I'll

play17:54

save this and when you save environment

play17:57

variables it will start a deploy

play17:59

automatically for you so let's just wait

play18:01

and see what happens here okay so it

play18:04

looks like everything is live so let's

play18:06

click on the link here and let's see if

play18:07

we can use the application and we see

play18:09

here it's now bad requests

play18:12

and because we see bad requests here we

play18:14

know something isn't working properly

play18:16

but we don't know exactly what the issue

play18:18

is so what we can do is we can go to the

play18:20

environment variable here we can edit

play18:22

the debug and we can put that to true so

play18:25

this will just be a momentary change so

play18:27

we can debug this and figure out what's

play18:28

going on so the app is going to deploy

play18:32

itself again okay so now that this is

play18:34

running we see a more detailed error

play18:37

message so we see Django render example

play18:39

app on render.com is an invalid host

play18:44

and the reason is is because we put too

play18:46

much in the environment variable so if I

play18:48

go to the environment variable here I

play18:50

have the HTTP s colon slash slash so let

play18:53

me remove that save it and then let the

play18:55

app deploy again and hopefully this is

play18:57

the last issue we have with this deploy

play18:59

process okay so it's deployed again now

play19:02

I can refresh here and now we see the

play19:05

name so everything is working properly

play19:06

with the app because it's using the same

play19:08

database as before we already have data

play19:11

so I can add a new member here and it

play19:13

will get saved and new member appears

play19:15

there so everything is working

play19:17

what I can do is I can change debug back

play19:19

to false and I just wanted to show you

play19:22

one more thing I can go down to settings

play19:24

and they have the option of having a

play19:27

custom domain I don't have any domains

play19:29

available but using a custom domain is

play19:31

pretty simple so you can just follow the

play19:33

instructions here and you'll have your

play19:35

own domain for your app instead of using

play19:37

the URL that they have here like this

play19:40

ugly one on render.com

play19:43

so that's it for this video that's all I

play19:44

want to show you I wanted to show you

play19:45

the deploy process and some of the

play19:47

common issues that you'll run into when

play19:49

you go to deploy a Django app or really

play19:51

any other app but this video was

play19:53

specifically Django so if you have any

play19:55

questions about anything that I've done

play19:56

this video feel free to leave a comment

play19:58

down below if you like this video please

play20:00

give me a thumbs up and if you are not

play20:02

yet a subscriber please subscribe to my

play20:03

channel so thank you for watching and I

play20:05

will talk to you next time

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Django DeploymentRender SetupPostgreSQLWeb ServicesGitHub IntegrationDatabase ConnectionDebuggingPython ProjectsWeb HostingApp Development