Code Monkey home page Code Monkey logo

sample-django's Introduction

Getting Started

These steps will get this sample Django application running for you using DigitalOcean. This application is the standard Django Polls Tutorial with an added page at /. This app uses a Postgres database by default. If you want to change this to use a local sqlite db (this will not persist deployments) then you'll need to set an environment variable specified below.

Note: Following these steps will result in charges for the use of DigitalOcean services

Requirements

Forking the Sample App Source Code

To use all the features of App Platform, you need to be running against your own copy of this application. To make a copy, click the Fork button above and follow the on-screen instructions. In this case, you'll be forking this repo as a starting point for your own app (see Github documentation to learn more about forking repos.

After forking the repo, you should now be viewing this README in your own github org (e.g. https://github.com/<your-org>/sample-django)

Deploying the App

  1. Visit https://cloud.digitalocean.com/apps (if you're not logged in, you may see an error message. Visit https://cloud.digitalocean.com/login directly and authenticate, then try again)
  2. Click "Launch App" or "Create App"
  3. Choose GitHub and authenticate with your GitHub credentials.
  4. Under Repository, choose this repository (e.g. <your-org>/sample-django) and click Next.
  5. On the next screen you will be prompted for the name of your app, which region you wish to deploy to, which branch you want deployments to spin off of and whether or not you wish to autodeploy the app every time an update is made to this branch. Fill this out according to how you want your app to function and click Next.
  6. Once you have reached the next screen you will be prompted about environment variables, build and run commands, and if you want to deploy a database.
    1. Click Edit next to the Environment Variables section and add the following :
      1. If you wish to launch the Django app in Debug mode, set DEBUG to True
      2. Set DJANGO_ALLOWED_HOSTS to ${APP_DOMAIN} to set the allowed_hosts setting of your app to the default domain provided by DigitalOcean.
      3. Set DATABASE_URL to ${<YOUR_DB_NAME>.DATABASE_URL}. You will set the database name in a next step. For simplicity, just name it db so the example would look like ${db.DATABASE_URL}
      4. You can find more information about all the different environment variables here.
  7. Modify the Run Command setting to point to your application. For this example my project is named mysite. So the modified command would be gunicorn --worker-tmp-dir /dev/shm mysite.wsgi.
  8. There is no need to modify the Build Command section
  9. Click Add a Database to add the necessary Postgres db. For simplicity sake, just name it db. You can also connect an existing DBaaS instance if you choose.
  10. Click "Next".
  11. Confirm your Plan settings and how many containers you want to launch and click Launch Basic/Pro App.
  12. You should see a "Building..." progress indicator. And you can click "Deployments"โ†’"Details" to see more details of the build.
  13. It can currently take 5-6 minutes to build this app, so please be patient. Live build logs are coming soon to provide much more feedback during deployments.
  14. Once the build completes successfully, click the "Live App" link in the header and you should see your running application in a new tab, displaying the home page.
  15. You will still need to perform the following tasks from the dashboard console to finish setting up your Django application:
    1. Navigate to the console tab
    2. Run python manage.py migrate to perform the initial database migration. After setup, you may need to migrate the database again if updated. For this reason, we recommend setting up a pre-deploy Job to migrate the database, automated to run before each deployment. This method also ensures that the migration happens successfully before serving traffic. If it fails, the old version of the service remains active instead.
    3. Run python manage.py createsuperuser and follow the prompts to create a super user to access at /admin.

Environment Variables

Many of the Django settings necessary to have an app run on App Platform have been exposed as environment variables. Below is a list and what they do

  • DEBUG - Set Django to debug mode. Defaults to False.
  • DJANGO_ALLOWED_HOSTS - The hostnames django is allowed to receive requests from. This defaults to 127.0.0.1,localhost. Can be a comma deliminated list. For more information about allowed_hosts view the django documentation.
  • DEVELOPMENT_MODE - This determines whether to use a Postgres db or local sqlite. Defaults to False therefore using the Postgres db
  • DATABASE_URL - The connection url including port, username, and password to connect to a postgres db. This is provided by App Platform. Required if DEVELOPMENT_MODE is False.

Making Changes to Your App

As long as you left the default Autodeploy option enabled when you first launched this app, you can now make code changes and see them automatically reflected in your live application. During these automatic deployments, your application will never pause or stop serving request because the App Platform offers zero-downtime deployments.

Here's an example code change you can make for this app:

  1. Edit code within the repository
  2. Commit the change to master. Normally it's a better practice to create a new branch for your change and then merge that branch to master after review, but for this demo you can commit to master directly.
  3. Visit https://cloud.digitalocean.com/apps and navigate to your sample-python app.
  4. You should see a "Building..." progress indicator, just like above.
  5. Once the build completes successfully, click the "Live App" link in the header and you should see your updated application running. You may need to force refresh the page in your browser (e.g. using Shift+Reload).

Learn More

You can learn more about the App Platform and how to manage and update your application at https://www.digitalocean.com/docs/apps/.

Deleting the App

When you no longer need this sample application running live, you can delete it by following these steps:

  1. Visit the Apps control panel at https://cloud.digitalocean.com/apps
  2. Navigate to the sample-python app
  3. Choose "Settings"->"Destroy"

This will delete the app and destroy any underlying DigitalOcean resources

Note: If you don't delete your app, charges for the use of DigitalOcean services will continue to accrue.

sample-django's People

Contributors

abe-101 avatar dependabot[bot] avatar elanhasson avatar fumblehool avatar kamaln7 avatar masonegger avatar ygabriel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sample-django's Issues

Multi line env vars continued

hi @kamaln7 with regards to #9

I'll close this ticket for now but please feel free to re-open if this still doesn't work for you.

Github does not allow me to re-open an issue that you closed. So, I opened a new one.

Please see my comment #9 (comment)

Multi-line env var is such a basic thing (so many things can have multi-line values), and I am surprised that DO does not support it. Am I doing something wrong ?

How to have multiline environment variables ?

In the digital ocean app platform deployment flow,

There is a step of editing environment variables.

We can set the key value pairs there.

But the value of some environment variables contain multiple lines. But digital ocean does not support that

How can I put such env vars ?

Hi, I got "Deploy Error: Non-Zero Exit Code"!

image

Here is the log:

[2021-10-13 04:53:53] => Initializing build
[2021-10-13 04:53:53] => Retrieving source code to /workspace
[2021-10-13 04:53:53] => Selecting branch "main"
[2021-10-13 04:53:54] => Checking out commit "87528640f02c13561376f79b9011ac724dc13ed9"
[2021-10-13 04:53:54] => Got source_dir: /
[2021-10-13 04:53:54] => Using workspace root /workspace
[2021-10-13 04:53:54] 
[2021-10-13 04:53:54] => Building app using buildpacks
[2021-10-13 04:53:55] => Injecting app environment variables:
[2021-10-13 04:53:55]      DJANGO_ALLOWED_HOSTS
[2021-10-13 04:53:55] => Running buildpack detection
[2021-10-13 04:53:55] 
[2021-10-13 04:53:55] 3 of 4 buildpacks participating
[2021-10-13 04:53:55] digitalocean/python-appdetect 0.0.2
[2021-10-13 04:53:55] heroku/python                 0.192.4
[2021-10-13 04:53:55] digitalocean/procfile         0.0.3
[2021-10-13 04:53:55] 
[2021-10-13 04:53:55] For documentation on the buildpacks used to build your app, please see:
[2021-10-13 04:53:55]    Python: https://do.co/apps-buildpack-python
[2021-10-13 04:53:56] 
[2021-10-13 04:53:56] => Building app
[2021-10-13 04:53:56] 
[2021-10-13 04:53:58] -----> Installing python-3.9.4
[2021-10-13 04:54:17] -----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
[2021-10-13 04:54:34] -----> Installing SQLite3
[2021-10-13 04:54:54] rm: cannot remove '/workspace/.heroku/python/apt/state/lists': Directory not empty
[2021-10-13 04:54:54] Sqlite3 failed to install.
[2021-10-13 04:54:55] -----> Installing requirements with pip
[2021-10-13 04:54:56]        Collecting asgiref==3.2.10
[2021-10-13 04:54:56]          Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
[2021-10-13 04:54:56]        Collecting Django==3.1.13
[2021-10-13 04:54:56]          Downloading Django-3.1.13-py3-none-any.whl (7.8 MB)
[2021-10-13 04:55:00]        Collecting gunicorn==20.0.4
[2021-10-13 04:55:00]          Downloading gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)
[2021-10-13 04:55:00]        Collecting psycopg2-binary==2.8.6
[2021-10-13 04:55:00]          Downloading psycopg2_binary-2.8.6-cp39-cp39-manylinux1_x86_64.whl (3.0 MB)
[2021-10-13 04:55:00]        Collecting pytz==2020.1
[2021-10-13 04:55:00]          Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
[2021-10-13 04:55:01]        Collecting sqlparse==0.3.1
[2021-10-13 04:55:01]          Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
[2021-10-13 04:55:01]        Collecting whitenoise==5.2.0
[2021-10-13 04:55:01]          Downloading whitenoise-5.2.0-py2.py3-none-any.whl (19 kB)
[2021-10-13 04:55:01]        Collecting dj-database-url==0.5.0
[2021-10-13 04:55:01]          Downloading dj_database_url-0.5.0-py2.py3-none-any.whl (5.5 kB)
[2021-10-13 04:55:01]        Installing collected packages: asgiref, sqlparse, pytz, Django, gunicorn, psycopg2-binary, whitenoise, dj-database-url
[2021-10-13 04:55:26]        Successfully installed Django-3.1.13 asgiref-3.2.10 dj-database-url-0.5.0 gunicorn-20.0.4 psycopg2-binary-2.8.6 pytz-2020.1 sqlparse-0.3.1 whitenoise-5.2.0
[2021-10-13 04:55:32] -----> $ python manage.py collectstatic --noinput
[2021-10-13 04:55:34]        135 static files copied to '/workspace/staticfiles', 423 post-processed.
[2021-10-13 04:55:34] 
[2021-10-13 04:55:58] 
[2021-10-13 04:55:58] => Uploading the built Docker image to the container registry...
[2021-10-13 04:56:05] Adding layer 'heroku/python:profile'
[2021-10-13 04:56:31] Adding 1/1 app layer(s)
[2021-10-13 04:56:31] Adding layer 'launcher'
[2021-10-13 04:56:31] Adding layer 'config'
[2021-10-13 04:56:31] Adding label 'io.buildpacks.lifecycle.metadata'
[2021-10-13 04:56:31] Adding label 'io.buildpacks.build.metadata'
[2021-10-13 04:56:31] Adding label 'io.buildpacks.project.metadata'
[2021-10-13 05:14:46] *** Images (sha256:7719de3c2dccbc6ddb4a30a2b2616d4a75bc4a5456b3b28c157572fe5f7d57c2):
[2021-10-13 05:14:46]       <image-1>
[2021-10-13 05:14:48] Layer cache not found
[2021-10-13 05:15:04] Adding cache layer 'heroku/python:shim'
[2021-10-13 05:33:21] => Uploaded the built image to the container registry
[2021-10-13 05:33:21] => Build complete
[]

the sample-django.yaml:

alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
name: sample-django
region: sgp
services:
- environment_slug: python
  envs:
  - key: DJANGO_ALLOWED_HOSTS
    scope: RUN_AND_BUILD_TIME
    value: ${APP_DOMAIN}
  github:
    branch: main
    deploy_on_push: true
    repo: imingx/sample-django
  http_port: 8080
  instance_count: 1
  instance_size_slug: basic-xxs
  name: sample-django
  routes:
  - path: /
  run_command: gunicorn --worker-tmp-dir /dev/shm mysite.wsgi
  source_dir: /

Not Clear on how to use static files via Digital Ocean App Platform

It was not clear on how to use static files via Digital Ocean App Platform, the YouTube video attached to this sample project is not clear about static files specifically. I thought that the DO Spaces or static sites were required but I have been able to get them working simply using white noise. Perhaps a follow up video will be useful. In the past I have used NGINX so I am not exactly sure how the Digital Ocean APP Platform is handling this. I am also curious still about how to connect static files using Digital Oceans CDN.

Would it require something like the following settings and environment variable?:

STATIC_HOST = os.environ.get('DJANGO_STATIC_HOST', '')
STATIC_URL = STATIC_HOST + '/static/'

DJANGO_STATIC_HOST=??

UPDATE To anyone interested I was able to get static files working using whitenoise and making sure the middleware and settings are as follows. And also making sure that collectstatic is not disabled or =1 on the DO App Platform.

'whitenoise.middleware.WhiteNoiseMiddleware',

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.