Will Murphy's personal home page

Django Raspberry Pi 3

Hello! This post is part of a series on building a Django app and deploying it on my Raspberry Pi. You may want to start with the first post in the series.

Last time, we got the web server to actually send requests to the django process, but it doesn’t serve static assets or have a database yet. Let’s check on the original checklist!

  1. Get our python code onto the Pi. DONE :)
  2. Get the dependencies installed on the Pi. DONE :)
  3. Configure apache webserver to forward requests to django. ALMOST!
  4. Set up a database
  5. Probably other things I haven’t thought of yet.

I visit the site in Firefox and open the dev tools. I see that the site tries to download all the static assets, but gets 404s. So the HTML references the static assets, but were not serving them correctly from the server. So this is still an apache2 problem, a django problem or an “apache2 not talking to django” problem.

To try to diagnose this, I ssh into the Pi and tail the apache server logs. I see a lot of errors in the logs like this: - - [19/Jun/2023:18:32:12 +0000] "GET
/static/admin/js/nav_sidebar.js HTTP/1.1" 404 496
"" "Mozilla/5.0 (Macintosh; Intel
Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/113.0"

Ok, so we’re looking for a file called nav_sidebar.js and not finding it. A reasonable question to ask is, “is this file on my server and if so where.” So let’s check!

$ find . -name nav_sidebar.js

That makes sense! I’m trying to view the admin page, which I didn’t build, and is part of django itself. So the static assets for it are in the directory where pip put all the django stuff when I did python -m pip install ..

After looking around for a while, I realized that I was missing a setting, and a django command. First off, I was missing STATIC_ROOT:

@@ -127,6 +127,7 @@ USE_TZ = True
 # https://docs.djangoproject.com/en/4.2/howto/static-files/

 STATIC_URL = "static/"
+STATIC_ROOT = path.join(BASE_DIR, "static")

I was surprised to need both these settings, so let’s figure out what the difference is! This post gives a pretty clear explanation, if you want to read more, but basically:

And, to get this going on the server, I need to run colllectstatic:

python manage.py collectstatic

Now if I re-run the find command:

$ find . -name nav_sidebar.js

You can see that the static assets from the django package got moved to ./static relative to my site root.

Let’s make sure that apache2 is configured to serve those:

<VirtualHost *:80>
    # ... snip ...
  Alias /static /home/pi/pidjango/static
    <Directory /home/pi/pidjango/static>
        Require all granted
    # ... snip ...

Looks good! And, sure enough, when I visit the browser now, I get nice layouts and such on /admin because the static assets are working.

Next time, we’ll try to set up the database!

Till then, happy learning!


Note: recently submitted comments may not be visible yet; the approval process is manual. Please be patient, and check back soon!

Join the conversation!