This challenge allows you to practice the concepts and techniques learned over the past week and apply them in a concrete project. This Sprint explored the basics of Django. During this Sprint, you studied how to setup a Django project, build a REST API, and use token authorization. In your challenge this week, you will demonstrate proficiency by taking the application that you built over the course of this week and deploying it! Getting your application out there is a great way to learn, shake out bugs, and get feedback as you can share it with others.
Read these instructions carefully. Understand exactly what is expected before starting this Sprint Challenge.
This is an individual assessment. All work must be your own. Your challenge score is a measure of your ability to work independently using the material covered through this sprint. You need to demonstrate proficiency in the concepts and objectives introduced and practiced in preceding days.
You are not allowed to collaborate during the Sprint Challenge. However, you are encouraged to follow the twenty-minute rule and seek support from your PM and Instructor in your cohort help channel on Slack. Your work reflects your proficiency in Python and Django and your command of the concepts and techniques covered during the Intro to Django Sprint.
You have three hours to complete this challenge. Plan your time accordingly.
Commit your code regularly and meaningfully. This helps both you (in case you ever need to return to old code for any number of reasons and your project manager.
In this challenge, you will be documenting your experience in creating a Django project and using Heroku to deploy the application you built over the course of this sprint.
In meeting the minimum viable product (MVP) specifications listed below, your application will be accessible remotely via Heroku's servers as demonstrated in the following screen-shot.
(placeholder img)
You will also create a short written piece documenting your experience.
This portion of the challenge is meant to allow you to practice your professional writing skills. In the tech world, that means writing things clearly, concisely, in a way to get the information across efficiently to an audience that may be pretty busy.
It is suggested that you limit your response to the questions below to one or two paragraphs. Keep your answers short and to the point. Consider how much of an online resource people typically view.
from How People Read Content Online
Demonstrate your understanding of this week's concepts by answering the following free-form questions. With your final submission, complete the DeploymentExperiences.md
file where you write about the experience of your Heroku deploy.
- Summarize the key steps in the deployment process.
- What went well?
- What challenges did you face?
- How far did you get?
- We have utilized docs from several different projects over the course of this Sprint.
- Which docs did you think were the most helpful?
- Name one or two specific things about this resource that you think sets it apart from other docs you have referenced in the past.
You are expected to be able to answer all these questions. Your responses contribute to your Sprint Challenge grade. Skipping this section will prevent you from passing this challenge.
Note: the instructions below assume you're on your master
branch in git.
The steps to deploy (at a high level) are:
-
Sign up for Heroku
-
Install the Heroku CLI
-
From your terminal,
heroku login
-
Get to your project/repo directory
-
Install new dependencies. (If using
virtualenv
, usepip install
as you have been, or migrate topipenv
.)pipenv install gunicorn
- the webserver for Heroku to use (rather than the one built-in to Django)pipenv install psycopg2-binary
- PostgreSQL client binariespipenv install dj-database-url
- enables parameterizing the database connection (so Heroku uses PostgreSQL but local is still SQLite)pipenv install whitenoise
- optimizes deployment of static files (you may not have any, but it's good to add this now)- If using
virtualenv
, you need to create arequirements.txt
file in your project root directory with the command:pip freeze > requirements.txt
-
Prepare your project
- Copy the
dotenv
file in this repository to.env
in your repository (this should not be checked in) ALLOWED_HOSTS
andDATABASE_URL
are probably already correct for your local environment, but read/understand them- Use the example code (you can just run it in a
python
repl) to generate a new secret key and changeSECRET_KEY
djorg/settings.py
will need new imports (from decouple import config
andimport dj_database_url
)- You can use
config
to load the environment variables you set above, e.g.SECRET_KEY = config('SECRET_KEY')
(ALLOWED_HOSTS
will be a little trickier, but that's why this is a sprint challenge!) - For the database, you want to both load the
DATABASE_URL
and pass it todj_database_url.config
(see documentation) - Make a
Procfile
(example) to tell Heroku what to run to start your app. (Hint: the name of your Django project is probably "djorg", not "gettingstarted".) - Configure
whitenoise
(add a few configuration lines to yoursettings.py
file per the documentation) - Add static settings to your
settings.py
:STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
- Copy the
-
heroku create your-app
- makes the project and adds Heroku as a remote to your git repository so you can push to it to deploy -
heroku addons:create heroku-postgresql:hobby-dev
- makes a PostgreSQL database associated with the project (and sets theDATABASE_URL
Heroku config var, equivalent to a local environment variable) -
Set the other Heroku config vars, e.g.
ALLOWED_HOSTS=.herokuapp.com
,DEBUG=False
, andSECRET_KEY=somenewsecret
- see the documentation, you can set either via the Heroku CLI or by logging in to the Heroku Dashboard in your browser -
Deploy! First, make sure you're all committed. Then
git push heroku master
Once you've got it deployed, you'll probably need to run migrations on Heroku
(since it's using a different database then local). You can do this with
heroku run python manage.py migrate
, and in general
heroku run python manage.py
is the way to do all the helpful Django things,
just "in production." For example, heroku run python manage.py createsuperuser
is a good thing to do, and you can even administer/explore the app/data with
heroku run python manage.py shell
and heroku run python manage.py dbshell
.
- Getting Started on Heroku with Python - official tutorial, has a full working example app if you want to step through with that before you try with Djorg
- Lambda School Python/Django resources
You may also find the heroku logs
command useful to diagnose errors, as Heroku
error messages are usually not that descriptive.
Note for bash users: if you ever get in a place where your terminal isn't
printing out newlines properly and is not echoing your keypresses, hit CTRL-C
then blindly type in the following command and hit RETURN
:
stty sane
If you get errors along these lines, make sure your STATICFILES_STORAGE
is set to Compressed
and not to Gzip
.
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Example working MIDDLEWARE
config:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'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',
]
Make sure the module is in your Pipfile
. If it's not, you probably installed
it with pip
instead of pipenv
.
If the module appears in pip list
:
pip uninstall modulename
pipenv install modulename
Then try to redeploy.
Error:
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
Try this in your settings.py
:
DATABASES = {}
DATABASES['default'] = dj_database_url.config(default=config('DATABASE_URL'), conn_max_age=600)
Another option:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
Your finished project must include all of the following requirements:
- A link to your Djorg project repo
- A link to your live site, if you were able to deploy
- Completed summary of your deployment process in
DeploymentExperiences.md
In your solution, it is essential that you follow best practices and produce clean and professional results. Schedule time to review, refine, and assess your work and perform basic professional polishing including spell-checking and grammar-checking on your work. It is better to submit a challenge that meets MVP than one that attempts too much and does not.
After finishing your required elements, you can push your work further. These goals may or may not be things you have learned in this module but they build on the material you just studied. Time allowing, stretch your limits and see if you can deliver on the following optional goals:
-
Teach others what you have learned about Django development. Write a blog post or short article. Consider easy-to-read formats like a how-to guide or top 5 list. Do NOT just publish your answers to the Self-Study/Essay Questions section. You may use this content as a starting point, but it must be expanded upon in a significant, transformative way. Or focus on one or two specific pieces of Django you explored this week and describe how to use them or why they are useful with a high level of technical detail.
Publish your writing on your own personal blog or website or submit it to a larger publication like Medium or Hackernoon. Show that you completed this stretch goal by submitting the following in a seperate folder in your repo name
stretch_write
:DjangoExperiences.md
file with the final text.- A link to the published piece OR screenshot showing your submitted it to be published on another site.
-
Explore how Heroku logging works. Try generating, retrieving, and filtering log messages from your app. Show that you completed this stretch goal by submitting the following in a seperate folder in your repo named
stretch_log
:- A short paragraph describing the steps you took to implement logs, saved in
LoggingExperiences.md
. Include some of the statements you wrote to generate logs from within your application. - A screenshot that show logs being viewed from the terminal.
- A second screenshot that shows logs being fetched with filtering arguments. Try fetching with filters at least two times, using different filters each time.
- A short paragraph describing the steps you took to implement logs, saved in