This is the code for the O'Reilly Video - Intro to Django presented by Arianne Dee.
You can download a PDF of the slides here.
- Install Python 3.11
- Check that Python was installed properly
- Choose an IDE
- Download the code
- Create a virtual environment
- Install Django
Feel free to email me at arianne.dee.studios@gmail.com if you are having any problems getting set up for the course.
This course uses Python 3.11, but Python 3.8 and higher should work.
The course uses Django 4.2, so check which Python versions are compatible with Django 4.2 here.
- Go to https://www.python.org/downloads/
- Click the yellow button at the top to download the latest version of Python.
Follow the prompts and install using the default settings.
The default settings don't add Python to your PATH so your computer doesn't know where to look for it when Python runs (for some inexplicable reason).
Follow the instructions here: Windows Python installer instructions
Follow the instructions here: Add Python to PATH variable in Windows
-
Open the PowerShell application in Windows or Terminal on Mac or Linux
-
Type
python --versionand press enter -
Type
python3 --versionand press enter -
Type
py --versionand press enter
At least one of those commands should print a Python version of 3.8 or higher (whichever version you just installed). If it doesn't, you have to follow instructions to add Python to your PATH variable.
PyCharm or VS Code are recommended.
For Django development, I recommend using PyCharm Professional Edition (paid). There is a 30-day free trial if you would like to try it out.
In the video I use the free PyCharm Community Edition, which is sufficient.
Download either version here: https://www.jetbrains.com/pycharm/download/
Install, open, and use the default settings.
Clone the repository.
- Click the green "Code" button at the top-right of the page
- Click "Download ZIP"
- Unzip it and move the intro-to-django-main folder to a convenient location
- In your console, navigate to the project folder (if you open the project in PyCharm or VSCode, the Terminal pane should already be located there)
- Using the python command from step 2, create a virtual environment
python -m venv django_venvorpython3 -m venv django_venv - Activate your virtual environment
- Mac/Linux:
source django_venv/bin/activate - PowerShell:
django_venv\Scripts\Activate.ps1
- Mac/Linux:
If you are new to virtual environments, please watch this video lesson
Once your virtual environment has been activated, install Django 3 using pip:
pip install djangoto install the latest version of Django
OR
pip install "django>=4.2,<5"to install the latest Django 4.2 version (once version 5+ is released)
No. Django 3+, does not support Python 2 or Python < 3.6.
On a Mac:
- Go to PyCharm > Preferences
On a PC:
- Go to File > Settings
Once in Settings:
- Go to Project: intro-to-django > Project Interpreter
- Look for your Python version in the Project Interpreter dropdown
- If it's not there, click gear icon > Add...
- In the new window, select System Interpreter on the left, and then look for the Python version in the dropdown
- If it's not there, click the ... button and navigate to your Python location
- To find where Python is located, look in these directories
- You may have to search the internet for where Python gets installed by default on your operating system
Here are some links to configure your Django project in the following IDEs
-
- My IDE of choice for working in Django
- Navigate into the
trivia_sitefolder - Create a virtual environment with Python 3.8+
- Activate your virtual environment
$ pip install --upgrade pipto upgrade pip$ pip install -r requirements/local.txtto install local requirements$ python manage.py migrateto migrate your database$ python manage.py createsuperuserand follow instructions$ python manage.py loaddata questionsto add seed data$ python manage.py runserverto run the development server
Live website at https://trivia-ariannedee.pythonanywhere.com/.
Hosted on Python Anywhere using Python 3.10 and MySQL 5.7.
To get a copy in production on your own server:
- Set up a server environment with Python 3.8 or higher and a database (Postgres or MySQL preferred)
- Fork this project or create a copy of the
trivia_sitefolder in your own repository and clone into your sever Note: The next few steps may differ depending on what kind of server/service you are using. Follow a Django setup tutorial if you can find one. - Create and activate a virtual environment if desired
- Set the
DJANGO_SETTINGS_MODULEenvironment variable totrivia_project.settings.production$ export DJANGO_SETTINGS_MODULE=trivia_project.settings.localon Linux- On PythonAnywhere, edit the WSGI configuration file with
os.environ['DJANGO_SETTINGS_MODULE'] = 'trivia_project.settings.production' - This makes sure running
manage.pyuses the right settings file
- Configure the server to use WSGI (instead of
python manage.py runserver)- Point to
trivia_project.wsgi.applicationor configure awsgi.pyfile on the server (follow tutorial instructions)
- Point to
- Set up your secrets in a
.envfile- Duplicate
.env.exampleand save it as.env - Fill in the
DJANGO_SECRET_KEYfield with a random string of 50+ characters - Fill in the
DB_PASSWORDfield - Email fields are only used for the forgot password feature. If you want to get it working, the easiest is to create a new Gmail address and create an app password for it and use that for
EMAIL_PASSWORD
- Duplicate
- Edit
trivia_site/trivia_project/settings/production.py- Update
ALLOWED_HOSTSto use your server address(es) - Edit your database settings (except password)
- Update
- Edit
trivia_site/requirements/production.txtto use the correct package for your database- Use a different
mysqlclientif necessary, orpsycopgorpsycopg2if using Postgres (or other package if using a different DB) - This might be trial and error. You can pip install it, try to get it working, then update the requirements file with the pinned version
- Use a different
- Install the requirements
$ pip install -r requirements/production.txt - Run/reload the server and see if it works, troubleshoot if necessary
- You may need to do more configuration to properly serve your static and media files. Look for a tutorial for your cloud provider or server type.
- If you cannot serve media files from the same server, use Amazon S3
- Migrate the database by running
python manage.py migrate - If that works, commit any code changes you made.
$ python manage.py createsuperuserand follow instructions$ python manage.py loaddata questionsto add seed data$ python manage.py collectstaticto collect the static files intostaticfiles/(run this every time your static files change)
Please let me know if you have any suggestions/updates/questions about these instructions.
Email me at
arianne.dee.studios@gmail.com
or submit an issue or pull request to this repository.