2023-12-09

Goal

To set up the HitHub App's Static Files and add debugging support to Environment Variables.

Notes

Installing psycopg2

I think I'll hold off on setting up the PostgreSQL database, or maybe even just use the SQLlite database that automatically comes with Django. I don't want to over-complicate the process of getting the development environment set up for new developers. It will also be useful to start seeing how easy it is for people on other machines to get a working version up and running. I know I still need to install psycopg2 regardless, so I had to start there.

It seems like this might be an issue with my current Python version. I had to re-install python because I was on python 3.10 and I wanted to use the most updated version of python 3.11. I used the command

Get-Command Python

in Powershell to see my computer was still referencing the old version of python. It looks like it was actually referencing my Inkscape version of python. Since I didn't have any configurations or settings on Inkscape I just uninstalled it (and will re-install it later when needed). I was able to successfully install the latest version of Python 3.11 (Python 3.11.7) by using the Microsoft Store. This also added it to my path which made things easier. I was able to verify that I could install psycopg2 without issues now that I was on python 3.11. I added this solution to stack overflow and continued with development.

I had to recreate the .venv folder because it was referencing python 3.10 and not python 3.11. I deleted the .venv folder and used the command

python -m venv .venv

to re-create it with the correct python version. This also matches the python version listed in the Dockerfile so that makes things easier. With this updated environment version, I now have to run the command

./.venv/Scripts/activate

to activate my Virtual Environment, but I'm now able to access and use pip effectively. Thankfully the requirements.txt file was handy so I installed dependencies from there using

pip install -r requirements.txt

and finally installed the latest version of psycopg2 using the command

pip install psycopg2-binary

Fixing Up / Prepping a Post-Anaconda DIMMiN

This means I'll need to create a Virtual Environment for the DIMMiN App as well (instead of using Anaconda) so I decided to do that really quick before I forget. This will be much easier than starting VS code all the time through anaconda. I created the virtual environment using

python -m venv .venv

then tried to install everything from requirements.txt but the build failed because I was using an old version of Pillow and psycopg2. I also couldn't run the server because I needed to set my KOMBU_FERNET_KEY. To do this I needed to set the key manually in my ./.venv/Scripts/activate.bat file with the command

set "KOMBU_FERNET_KEY=your_generated_key"

but it turns out I needed to actually add it to the ./.venv/Scripts/Activate.ps1 file instead. This got things up and running! I also found out that when I'm in VS code, All I need to use is activate to activate the .venv Virtual Environment.

Static file setup

With that out of the way and solved (and my Python version now completely up to date) I could now begin setting up my Static Files in my HitHub App. I referenced my DIMMiN App to set the staticfile settings to the following:

STATIC_URL = '/static/'
STATIC_ROOT = str(BASE_DIR.joinpath('static'))
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_common'), # Static files used by the entire project
]

Where the only change I made was to try out the whitenoise.storage.CompressedManifestStaticFilesStorage instead of Django's default.

I could try collecting staticfiles by activating the environment with activate and running python manage.py collectstatic, however I wanted to get into the habit of running Docker commands instead and decided to start up docker with

docker-compose up -d

then use

docker-compose exec web python manage.py collectstatic

but for some reason that didn't work so I just activated the venv and used that. For some reason when I ran collectstatic it wasn't able to pull files from my other apps. That was because I didn't actually add my new apps to my INSTALLED_APPS parameter of my hithub/settings.py file. After adding my new apps it was able to find the staticfiles and woked. Nice!

Establishing Templates

People seem to want to work on the front-end of the site, so I want to start building out the Django Templates as soon as possible. I created a folder called templates and added this directory in my hithub/settings.py file in the TEMPLATES variable. I created an _base.html file with an _ prefix because that denotes that the file is solely meant to be inhereted by other files (according to Django For Professionals).

Since I wanted to use the fights page as my home page I added it to the project-level hithub/urls.py file as

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("fights.urls")) # Establish home page here
]

Then I created a fights/urls.py file and added in the correct routes via

from django.urls import path
from .views import FightPageView

urlpatterns = [
    path("", FightPageView.as_view(), name="home"),
    path("fights/", FightPageView.as_view(), name="fights")
]

And finally created the view to render the actual HTML page with

class FightPageView(TemplateView):
    template_name = "fights.html"

which could now render my page correctly. I added an about app and configured all of my other apps to route to the correct template. Now I can access all of my templates through the URLs correctly. Yay!

I decided to add these changes and commit them to github.

Results

Next Time


Previous Note 2023-12-07 Next Note 2023-12-10