README Outline:
- Chapters 1-3 build the blog (assignment)
- Chapters 4-7 build the bookmarks social website (assignment)
- Chapters 8-11 build an ecommerce application (assignment)
- Chapters 12-18 build a CMS for an online elearning platform (basis of final project)
- Technical Notes CH 2 - 7 - Published October 2024
- Docker - Part 1
- Emails and Email Backends - Part 2
- Docker and Postgres - Part 3
- Social SSO - Part 4
- Methods to implement Recipe (ch 1 - 3) - Not published yet
- Working with Bootstrap - Not published yet
- Technical Notes CH 8 - 11
- Celery and Rabbit MQ - Part 1
Steps from https://anymail.dev/en/v12.0/esps/sendgrid/
pip install "django-anymail[sendgrid]"
- add
anymail
to INSTALLED_APPS
WSL2 is probably the best option for Windows.
You can use the shell from within the Docker Interface
Or, if on Windows, use PowerShell. If on Mac, Terminal is fine.
docker run --name=blog_db2 -e POSRGRES_DB=blog -e POSTGRES_USER=blog -e POSTGRES_PASSWORD=xxxxx -p 5432:5432 -d postgres:latest
steps:
docker pull postgres
- I left the version off, so it gets the latestdocker run --name=blog_db -e POSRGRES_DB=blog -e POSTGRES_USER=blog -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:latest
- note, you will realize the best results if you just stick to what Antonio is showing.- Although running
pip install psycopg
is a safe approach, it may not work entirely when doing a Docker container with Postgres, this mainly due to Postgres being virtualized where none of the resources used by Postgres are actually on your local system (Docker is). Your likely error message here will be "psycopg or psycopg2 not installed/found." - So, in case of issues with step 3 above, try this:
pip install "psycopg[binary]"
- this is recommended in the psycopg documentation. - Its also possible to rent a Postgres server from a number of places such as Digital Ocean.
- The best way to see if all of this goes well is to run
python manage.py check
- this checks to see if Django will properly "bootstrap" itself to life. A good resposne is "System check identified no issues (0 silenced)." dumpdata
andloaddata
may have failed depending on whether you followed the book exactly. Not a huge deal in development.- You can run/pause/stop the Postgres Docker container using the desktop application.
Clearly, the screens at the Developer Console for Google's Cloud Services are slightly different than the book. However, the screens are similar.
Other notes:
- It is CRITICAL that, in Windows, the hosts file is edited with a text editor that is started with evelated permissions (right click and "Run as Administrator")
- the Client ID from the "Client ID for Web Application" screen is the GOOGLE_OAUTH2_KEY
- the Client secrete from from the "Client ID for Web Application" is the GOOGLE_OAUTH2_SECRET
- django social auth is doing the rest of the "magic"
- The URL for the overall cloud developer console is: Google Cloud Console
- A new project screen:
- Select your project:
- Select OAuth Consent screen:
- Enter App Info (name and Authorized Domains):
- Add Test User (must be the email you want to social auth with):
- Create Credentials (dropdown shown and different from book):
- Create OAuth Cliend ID - Application Type "Web Application"; Add Authorized JavaScript Origings: Add Authorized redirect URIs:
- Results Dialog - all info needed are on results diaglog:
- Credentials Dashboard -
- Credentials Summary -
RabbitMQ RabbitMQ will run via Docker. The commands in the book are straightforward:
- Make sure Docker Desktop is running
- grab the latest rabbitmq-management image:
docker pull rabbitmq:management
- run the image in a container:
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management
Celery
- On Windows, you will likely need to run a separate Git-Bash session with administrative rights (right-click and "Run as Administrator")
- If not you will likely get this error
[WinError 5] Access Is Denied
, however, that is not the only source of this error on windows.
Let's look further into this:
- For now, don't run Celery within VS Code (on windows) until I find a VS Code setting for you.
- You WILL need to activiate the virtual environment in this new (separate) Git-Bash or PowerShell terminal
- You will also want to limit the number of concurrent worker to something small for our testing purposes.
- Lastly, Windows has a hard time spawning new subprocesses and will utilizately thrlow the Access Denied error.
- Although not mentioned in the book (because Antonio is likely developing on a Mac), we can use a Python "coroutine" trick using the gevent package
pip install gevent
which utilizes greenlet to "fake" Python into using a single process as threads or coroutines. After installinggevent
we can move on (as far as I can tell, this is Windows stuff). - So, try adjusting the command to the following:
celery -A myshop worker --loglevel=INFO --concurrency=4 -P gevent
and you should achieve success where the errors disappear.
You will be left with output that looks like the book, but the outlet is on the SEPARATE "Run As Admin" window.
pip install gevent
celery -A myshop worker --loglevel=info -P gevent
In the end, sending an email via a message queue like celery and RabbitMQ is going to be a "nice to have" feature in the project.
Again, in many cases, getting things to work on Windows can be, different. This is because most web deployment environments are not Windows based but rather *nix (Linux/Unix/OSX) based.
Doing the pip install WeasyPrint
from the book is not going to work for Windows folks.
The truth is that the steps at the WeasyPrint website's documentation are the way to go.
At issue with these instructions is a vital additional set of steps discussed here: https://stackoverflow.com/a/78715011
- Install MSYS2 https://www.msys2.org/#installation
- After installation MSYS2 execute the following command in MSYS2 shell:
pacman -S mingw-w64-x86_64-pango
- Then add path
C:\msys64\mingw64\bin
to enviroment variables and of course add weasyprint to your project. If you don't have experience with this, there are "how to" resouces. - You will find this now works and the easy way to check is (with your virtual environment activated):
python manage.py check
Again, we are faced with the following question: so, is WeasyPrint and the ability to print to PDF wor it worth it? It depends, but I'm putting it as a "nice to have" feature. The trick here is to skip those parts.
I think those on OSX will have an easier time.
Do not BOG DOWN.
In chapter 11, we again find ourselves faced with challenges as windows users. This speaks volumes about how the web is run as most HTTP servers execute in a Unix/Linux environment (of which Mac OSX is a part of). As windows users, Antonio tells us to:
- Read the documentation about using
gettext
on Windows at the Django Documentation - Once at the Django documentation, we are encouraged to download a pre-compiled binary. Here are there are four choices:
Of those, almost all of us are using a 64-bit windows, and the static build is likely the safest. In all cases, I elected to use the installer as it will put things in the right place and offer this critical dialog with the needed options:
Sometimes, most of the time, it is a good idea to reboot your computer now as even though this installer should have included the installation directory into your PATH variable, this may not take effect until a reboot. I talk above setting the PATH in an earlier section of this README. NOTE: Rebooting is what made django-admin makemessages --all
work for me after failing before a reboot.
FINAL NOTE on the ONLINE SHOP Project
For all of the functions in the online shop example, it is expected that the following are always running:
- Docker Desktop
- RabbitMQ
- Celery
- A proper Stripe API connection
The shop won't run without these. I know these external dependencies are a pain, but they represent a very realistic scenario for you to understand.
Structure of project:
Subject 1
Course 1
Module 1
Content 1 (image)
Content 2 (text)
Module 2
Content 3 (text)
Content 4 (file)
Content 5 (video)
...
I don't specify a version of memchached, so it defaults to latest
:
docker pull memcached
- defaults to latest
docker run -it --rm --name memcached -p 11211:11211 memcached:latest -m 64
Also for redis:
docker run -it --rm --name redis -p 6379:6379 redis:latest
-
DO NOT SKIP READING THE BOOK! Most of the tips, and links at the back of the chapter, provide VITAL clues.
-
DO use Antonio's GitHub Repostitory for book code.
-
We eventually start running the Django debug server wit this command:
python manage.py runserver_plus --cert-file cert.crt
-
The Django Secret Key Generator - Source Code for the curious - you need this because the default secret key is not meant for deployment.
-
Commands to run when checking for outdated packages in your Virtual Environment (VE):
- Be sure that your VE is activated (
source .venv/Scripts/activate
) - check for outdated packages
python -m pip --outdated
- update package(s) as appropriate
python -m pip install --upgrade <name-of-package>
- Be sure that your VE is activated (
-
In addition to the book, this is a good resource: W3Schools Django
-
A Git Cheat Sheet is available as a quick reference for common
git
commands. -
Boostrap Crash Course (freecodecamp): Full Bootstrap 5 Tutorial for Beginners
- Templates and Bootstrap - I will strongly recommend that you use and develop familiarity with Bootstrap as a CSS framework for all of your work in this class.
- Classy Class-Based Views - Should you wish to go in this direction: https://ccbv.co.uk/
- Environment Variables - I have used python-dotenv forever and have done so in my code - Antonio uses python-decouple.
- Candidate Heroku Replacements - Heroku used to be the best free Django deployment platform - it is no longer free. Here are some replacement services.
- render.io
- python anywhere
- Fly.io
- Railway
This README file is constructed using MarkDown. Here is another good Markdown reference.