How to Upload a Django Project to Github
Django is a powerful web framework that allows you to deploy your Python applications or websites. Django includes many features such as authentication, a custom database ORM (Object-Relational Mapper), and an extensible plugin architecture. Django simplifies the complexities of spider web development, allowing yous to focus on writing code.
In this tutorial, you'll configure a Django project and deploy it to DigitalOcean's App Platform using GitHub.
Prerequisites
To complete this tutorial, you'll need:
- A GitHub business relationship.
- Python3 installed on your local machine. You can follow the following tutorials for installing Python on Windows, Mac, or Linux.
- A text editor. Y'all can employ Visual Studio Code or your favorite text editor.
Footstep ane: Creating a Python Virtual Environment for your Project
Before you get started, you need to ready our Python developer environment. You will install your Python requirements inside a virtual environment for easier management.
Starting time, create a directory in your home directory that you tin can use to store all of your virtual environments:
At present create your virtual environment using Python:
python3 -chiliad venv ~/.venvs/django
This will create a directory chosen <^>django<^>
within your <^>.venvs<^>
directory. Inside, it will install a local version of Python and a local version of pip
. You can utilise this to install and configure an isolated Python environment for your project.
Before you lot install your project's Python requirements, you need to activate the virtual environment. Yous can do that by typing:
source ~.venvs/django/bin/activate
Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look something like this: (<^>django<^>)<^>user<^>@<^>host<^>:~$
.
With your virtual environment active, install Django, Gunicorn, dj-database-url, and the psycopg2
PostgreSQL adaptor with the local case of pip
:
pip install django gunicorn psycopg2-binary dj-database-url
Note: When the virtual environment is activated (when your prompt has (django)
preceding information technology), use pip
instead of pip3
, even if you are using Python iii. The virtual environment's copy of the tool is ever named pip
, regardless of the Python version.
These packages practice the following:
-
django
- Installs the Django framework and libraries -
gunicorn
- A tool for deploying Django with a WSGI -
dj-database-url
- A Django tool for parsing a database URL -
psycopg2
- A PostgreSQL adapter that allows Django to connect to a PostgreSQL database
Now that y'all accept these packages installed, you volition need to save these requirements and their dependencies so App Platform can install them later. You can practise this using pip
and saving the information to a requirements.txt file:
pip freeze > requirements.txt
You should now accept all of the software needed to commencement a Django projection. You lot are almost prepare to deploy.
Stride 2: Creating the Django Project
Create your project using the django-admin tool that was installed when you installed Django:
django-admin startproject <^>django_app<^>
At this indicate, your electric current directory (<^>django_app<^>
in your example) will have the following content:
-
manage.py
: A Django project direction script. -
django_app/
: The Django project parcel. This should contain the__init__.py
,settings.py
,urls.py
,asgi.py
, andwsgi.py
files.
This directory is the root directory of your project and will exist what we upload to GitHub. Navigate into this directory with the command:
Let's adjust some settings before deployment.
Adjusting the Project Settings
Now that you've created a Django projection, you'll demand to modify the settings to ensure it will run properly in App Platform. Open the settings file in your text editor:
nano <^>django_app<^>/settings.py
Let's examine our configuration one step at a time.
Reading Environment Variables
First, you lot need to add the os
import statement to be able to read environment variables:
[label django_app/settings.py] import bone
Setting the Secret Key
Next, you need to modify the SECRET_KEY
directive. This is set up past Django on the initial project creation and will take a randomly generated default value. Information technology is unsafe to go on this hardcoded value in the lawmaking once it's pushed to GitHub, so y'all should either read this from an surroundings variable or generate it when the application is started. To exercise this, add together the following import statement at the height of the settings file:
[characterization django_app/settings.py] from django.cadre.management.utils import get_random_secret_key
Now modify the SECRET_KEY
directive to read in the value from the environs variable DJANGO_SECRET_KEY
or generate the primal if it does not find said surroundings variable:
[label django_app/settings.py] SECRET_KEY = bone.getenv("DJANGO_SECRET_KEY", get_random_secret_key())
Warning: If you lot don't set this environment variable, then every time the app is re-deployed, this fundamental will change. This can have adverse furnishings on cookies and volition require users to log in once more every time this fundamental changes. You can generate a key using an online password generator.
Setting Allowed Hosts
At present locate the ALLOWED_HOSTS
directive. This defines a list of the server'south addresses or domain names that may be used to connect to the Django instance. Any incoming request with a Host header that is not in this list will raise an exception. Django requires that you lot gear up this to prevent a certain class of security vulnerability.
In the square brackets, list the IP addresses or domain names that are associated with your Django server. Each detail should exist listed in quotations with entries separated past a comma. If you wish requests for an entire domain and any subdomains, prepend a period to the beginning of the entry.
App Platform supplies yous with a custom URL every bit a default and then allows you to fix a custom domain subsequently y'all have deployed the application. Since you won't know this custom URL until you accept deployed the awarding, you should endeavour to read the ALLOWED_HOSTS
from an environment variable, so App Platform can inject this into your app when it launches.
We'll cover this process more than in-depth in a after section. Simply for now, change your ALLOWED_HOSTS
directive to attempt to read the hosts from an environment variable. The surround variable can be set to either a single host or a comma-delimited list:
[label django_app/settings.py] ALLOWED_HOSTS = bone.getenv("DJANGO_ALLOWED_HOSTS", "127.0.0.1,localhost").split up(",")
Setting the Debug Directive
Next you should alter the DEBUG
directive so that yous can toggle this past setting an environment variable:
[label django_app/settings.py] DEBUG = os.getenv("DEBUG", "False") == "Truthful"
Here y'all used the getenv
method to check for an environment variable named DEBUG
. If this variable isn't found, we should default to Imitation
for safe. Since environment variables will be read in as strings from App Platform, exist sure to make a comparison to ensure that your variable is evaluated correctly.
Setting the Evolution Manner
Now create a new directive named DEVELOPMENT_MODE
that volition also exist set equally an environment variable. This is a helper variable that you will use to decide when to connect to your Postgres database and when to connect to a local SQLite database for testing. You lot'll use this variable later when setting up the database connection:
[label django_app/settings.py] DEVELOPMENT_MODE = os.getenv("DEVELOPMENT_MODE", "Faux") == "Truthful"
Configuring Database Access
Next, notice the section that configures database access. It will start with DATABASES
. The configuration in the file is for a SQLite database. App Platform allows yous to create a PostgreSQL database for our projection, so y'all need to adjust the settings to be able to connect to it.
Warning: If you don't modify these settings and continue with the SQLite DB, your database will exist erased after every new deployment. App Platform doesn't maintain the disk when re-deploying applications, and your data will be lost.
Modify the settings with your PostgreSQL database data. You lot'll read in the database connection information and credentials from an environment variable, DATABASE_URL
, that will be provided past App Platform. Use the psycopg2
adaptor nosotros installed with pip
to have Django access a PostgreSQL database. You'll use the dj-database-url
package that was installed to get all of the necessary information from the database connection URL.
To facilitate with development of your application locally, you lot'll also apply an if
statement hither to determine if DEVELOPMENT_MODE
is set to True
and which database should be accessed. By default, this volition exist set to Fake
, and it volition effort to connect to a PostgreSQL database. You lot also don't want Django attempting to make a database connection to the PostgreSQL database when attempting to collect the static files, so yous'll write an if
argument to examine the control that was executed and non connect to a database if you determine that the command given was collectstatic
. App Platform will automatically collect static files when the app is deployed.
Offset, install the sys
library then you lot can determine the control that was passed to manage.py
and the dj_database_url
library to be able to parse the URL passed in:
[label django_app/settings.py] . . . import os <^>import sys<^> <^>import dj_database_url<^>
Next remove the current DATABASE
directive block and replace it with this:
[label django_app/settings.py] if DEVELOPMENT_MODE is True: DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", "Proper name": bone.path.join(BASE_DIR, "db.sqlite3"), } } elif len(sys.argv) > 0 and sys.argv[1] != 'collectstatic': if os.getenv("DATABASE_URL", None) is None: heighten Exception("DATABASE_URL surroundings variable not divers") DATABASES = { "default": dj_database_url.parse(bone.environ.get("DATABASE_URL")), }
Next, move down to the bottom of the file and add a setting indicating where the static files should be placed. When your Django app is deployed to App Platform, python manage.py collectstatic
will be run automatically. Set the road to match the STATIC_URL
directive in the settings file:
[characterization django_app/settings.py] . . . STATIC_URL = "/static/" <^>STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")<^>
If yous plan on storing static files in other locations exterior of your private Django-app static files, yous will need to add together an additional directive to your settings file. This directive will specify where to find these files. Be aware that these directories cannot share the aforementioned name as your STATIC_ROOT
. If y'all do not have extra static files do not include this setting.:
[label django_app/settings.py] . . . STATIC_URL = "/static/" STATIC_ROOT = bone.path.join(BASE_DIR, "staticfiles") <^>STATICFILES_DIRS = (bone.path.bring together(BASE_DIR, "static"),)<^>
Reviewing the Completed settings.py
File
Your completed file will look like this:
from django.core.management.utils import get_random_secret_key from pathlib import Path import os import sys import dj_database_url # Build paths inside the projection like this: BASE_DIR / 'subdir'. BASE_DIR = Path ( __file__ ) . resolve () . parent . parent # Quick-showtime development settings - unsuitable for product # See https://docs.djangoproject.com/en/three.i/howto/deployment/checklist/ # SECURITY Alert: keep the secret key used in production clandestine! SECRET_KEY = bone . getenv ( "DJANGO_SECRET_KEY" , get_random_secret_key ()) # SECURITY Warning: don't run with debug turned on in production! DEBUG = bone . getenv ( "DEBUG" , "Fake" ) == "Truthful" ALLOWED_HOSTS = os . getenv ( "DJANGO_ALLOWED_HOSTS" , "127.0.0.1,localhost" ) . split ( "," ) # Awarding definition INSTALLED_APPS = [ 'django.contrib.admin' , 'django.contrib.auth' , 'django.contrib.contenttypes' , 'django.contrib.sessions' , 'django.contrib.messages' , 'django.contrib.staticfiles' , ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware' , 'django.contrib.sessions.middleware.SessionMiddleware' , 'django.middleware.common.CommonMiddleware' , 'django.middleware.csrf.CsrfViewMiddleware' , 'django.contrib.auth.middleware.AuthenticationMiddleware' , 'django.contrib.messages.middleware.MessageMiddleware' , 'django.middleware.clickjacking.XFrameOptionsMiddleware' , ] ROOT_URLCONF = 'django_app.urls' TEMPLATES = [ { 'BACKEND' : 'django.template.backends.django.DjangoTemplates' , 'DIRS' : [], 'APP_DIRS' : True , 'OPTIONS' : { 'context_processors' : [ 'django.template.context_processors.debug' , 'django.template.context_processors.request' , 'django.contrib.auth.context_processors.auth' , 'django.contrib.messages.context_processors.messages' , ], }, }, ] WSGI_APPLICATION = 'django_app.wsgi.awarding' # Database # https://docs.djangoproject.com/en/3.1/ref/settings/#databases DEVELOPMENT_MODE = bone . getenv ( "DEVELOPMENT_MODE" , "False" ) == "True" if DEVELOPMENT_MODE is True : DATABASES = { "default" : { "ENGINE" : "django.db.backends.sqlite3" , "NAME" : os . path . join ( BASE_DIR , "db.sqlite3" ), } } elif len ( sys . argv ) > 0 and sys . argv [ 1 ] != 'collectstatic' : if os . getenv ( "DATABASE_URL" , None ) is None : enhance Exception ( "DATABASE_URL environment variable not defined" ) DATABASES = { "default" : dj_database_url . parse ( os . environ . get ( "DATABASE_URL" )), } # Password validation # https://docs.djangoproject.com/en/iii.1/ref/settings/#auth-countersign-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME' : 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator' , }, { 'NAME' : 'django.contrib.auth.password_validation.MinimumLengthValidator' , }, { 'Name' : 'django.contrib.auth.password_validation.CommonPasswordValidator' , }, { 'Proper name' : 'django.contrib.auth.password_validation.NumericPasswordValidator' , }, ] # Internationalization # https://docs.djangoproject.com/en/3.one/topics/i18n/ LANGUAGE_CODE = 'en-united states' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/iii.1/howto/static-files/ STATIC_URL = "/static/" STATIC_ROOT = os . path . join ( BASE_DIR , "staticfiles" ) # Uncomment if you accept extra static files and a directory in your GitHub repo. # If you don't have this directory and have this uncommented your build volition fail # STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
Note: At that place are values inside settings.py
that are specific to your project (such as WSGI_APPLICATION
and ROOT_URLCONF
) that are generated when you lot starting time setup your app. If y'all named your app something other than django_app
and are going to copy and paste this code direct exist sure to change these settings to match your project. They will have been fix correctly in the settings.py
that was generated for y'all.
Save and close settings.py
.
You lot've at present finished configuring your Django app to run on App Platform. Next, yous'll button the app to GitHub and deploy information technology to App Platform.
Step 3: Pushing the Site to GitHub
DigitalOcean App Platform deploys your code from GitHub repositories, so the commencement affair you lot'll demand to do is go your site in a git
repository so push button that repository to GitHub.
Showtime, initialize your Django projection as a git
repository:
When y'all work on your Django app locally, certain files get added that are unnecessary for deployment. Let'south exclude that directory by adding it to Git'southward ignore list. Create a new file called .gitignore
:
At present add the following code to the file:
[characterization .gitignore] db.sqlite3 *.pyc
Save and close the file.
Now execute the following command to add files to your repository:
git add django_app/ manage.py requirements.txt static/
Make your initial commit:
git commit -k "Initial Django App"
Your files will commit:
[secondary_label Output] [main (root-commit) eda5d36] Initial Django App viii files changed, 238 insertions(+) create manner 100644 django_app/__init__.py create mode 100644 django_app/asgi.py create style 100644 django_app/settings.py create mode 100644 django_app/urls.py create mode 100644 django_app/wsgi.py create style 100644 manage.py create mode 100644 requirements.txt create way 100644 static/README.md
Open your browser and navigate to GitHub, log in with your profile, and create a new repository called django-app
. Create an empty repository without a README
or license file.
In one case you've created the repository, return to the command line to push your local files to GitHub.
First, add together GitHub every bit a remote repository:
git remote add origin https://github.com/<^>your_username<^>/django-app
Side by side, rename the default branch main
, to match what GitHub expects:
Finally, push your master
branch to GitHub'due south main
branch:
Your files will transfer:
[secondary_label Output] Enumerating objects: 12, done. Counting objects: 100% (12/12), done. Delta compression using upwards to 8 threads Compressing objects: 100% (9/ix), washed. Writing objects: 100% (12/12), 3.98 KiB | 150.00 KiB/s, done. Total 12 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (one/1), done. To github.com:<^>yourUsername<^>/django-app.git * [new co-operative] main -> primary Co-operative 'chief' gear up to rails remote branch 'master' from 'origin'.
Enter your GitHub credentials when prompted to push button your lawmaking.
Your lawmaking is now on GitHub and accessible through a web browser. Now you lot will deploy your site to DigitalOcean'south App Platform.
Pace 4: Deploying to DigitalOcean with App Platform
One time the code is pushed, visit https://cloud.digitalocean.com/apps and click Launch Your App. Yous'll be prompted to connect your GitHub account.
Connect your business relationship and let DigitalOcean to admission your repositories. You tin cull to let DigitalOcean accept access to all of your repositories or simply to the ones you wish to deploy.
Click Install and Qualify. You'll exist returned to your DigitalOcean dashboard to continue creating your app.
Once your GitHub account is connected, select the <^>your_account<^>/django-app
repository and click Side by side.
Adjacent, provide your app's proper name, choose a region, and ensure the master
branch is selected. And so ensure that Autodeploy lawmaking changes is checked. Click Next to go on.
DigitalOcean will find that your project is a Python app and will automatically populate a partial run command.
Click the Edit link side by side to the Build and Run commands to complete the build command. Your completed build command needs to reference your projection'due south WSGI file. In this example, this is at django_app.wsgi
. Your completed run control should be gunicorn --worker-tmp-dir /dev/shm django_app.wsgi
.
Next, you need to ascertain the environment variables you declared in your project's settings. App Platform has a concept of App-Wide Variables
, which are surround variables that are provided by App Platform, such as APP_URL
and APP_DOMAIN
. The platform also maintains Component-Specific Variables
, which are variables that are exported from your components. These will be useful for determining your APP_DOMAIN
beforehand so you tin can properly set DJANGO_ALLOWED_HOSTS
. You will also employ these variables to re-create configuration settings from your database.
To read more about these different variables, consult the App Platform Environment Variable Documetation
For your Django app to function, you need to set the following environment variables like so:
-
DJANGO_ALLOWED_HOSTS
->${APP_DOMAIN}
- This allows us to know the randomly generated URL that App Platform provides and provide it to our app
-
DATABASE_URL
->${<NAME_OF_YOUR_DATABASE>.DATABASE_URL}
- In this instance, nosotros'll name our database db in the next footstep, so this should exist
${db.DATABASE_URL}
- In this instance, nosotros'll name our database db in the next footstep, so this should exist
-
DEBUG
->True
- Set this to
True
for at present to verify your app is functioning and ready toFake
when it's time for this app to exist in production
- Set this to
-
DJANGO_SECRET_KEY
-><A RANDOM SECRET Cardinal>
- You can either allow your app to generate this at every launch or choice a strong password with at least 32 characters to employ equally this key. Using a secure password generator is a good selection for this
- Don't forget to click the Encrypt check box to ensure that your credentials are encrypted for prophylactic
To gear up up your database, click the Add a Database button. You will be presented with the option of selecting a small development database or integrating with a managed database elsewhere. For this deployment, select the development database and ensure that the name of the database is db
. One time yous've verified this, click the Add together Database push.
Click Next, and you'll be directed to the Finalize and Launch screen where yous'll choose your plan. Exist sure to select the appropriate plan to fit your needs, whether in Basic App or Professional person App and then click Launch App at the bottom. Your app will build and deploy:
In one case the build process completes, the interface will prove y'all a healthy site. At present you need to admission your app's panel through the Console tab and perform the Django first launch tasks past running the post-obit commands:
-
python manage.py migrate
- This will perform your initial database migrations. -
python manage.py createsuperuser
- This volition prompt you for some information to create an administrative user.
Once you are done with that, click on the link to your app provided by App Platform.
This link should take you to the standard initial Django folio.
And now you take a Django app deployed to App Platform. Any changes that you make and push button to GitHub will be automatically deployed.
Step five: Deploying Your Static Files
At present that you've deployed your app, y'all may find that your static files aren't being loaded if you have DEBUG
set to False
. Django doesn't serve static files in production and instead wants you to deploy them using a web server or CDN. Luckily, App Platform can do just that. App Platform provides free static asset serving if you are running a service alongside it, as you are doing with your app. So you lot're going to deploy your same Django app merely as a static site this fourth dimension.
Once your app is deployed, click the Deportment button and choose Create Resources to add a static site component.
Select the aforementioned GitHub repository as your deployed Django service. Click Next to continue.
Next, provide your app's proper name and ensure the main
branch is selected. Click Next to keep.
Your component will be detected as a Service, and then you'll want to change the blazon to Static Site. Essentially we'll take Django gather our static files and serve them. Set the route to what you set your STATIC_URL
directive in your settings file. We set our directive to /static/
then set the route to /static
. Finally, your static files will be collected into Output Directory in your app to match your STATIC_ROOT
setting in settings.py
. Yours is set to staticfiles
, so set Output Directory to that.
Click Next, and yous'll exist directed to the Finalize and Launch screen. When static files are paired with a service, it is complimentary, so you won't come across any change in your bill. Click Launch App and deploy your static files. At present, if you lot have Debug set to False, you'll see your static files properly displayed.
Summary
In this tutorial, you:
- Gear up a Django project.
- Configured the Django project to connect to a database.
- Deployed the app and its static files onto App Platform.
Any changes yous commit and push button to your repository will be re-deployed, and so y'all tin can now expand your application. You can observe the case code for this tutorial in the DigitalOcean Sample Images Repository.
The example in this tutorial is a minimal Django project. Your app might have more than applications and features, but the deployment process will exist the same.
What's Next?
Once you take deployed the app and used it, yous can delete the application or deploy other sample applications.
Source: https://docs.digitalocean.com/tutorials/app-deploy-django-app/
0 Response to "How to Upload a Django Project to Github"
Post a Comment