Jump to content

[Solved] Django app returns 404: not found for static files, how to fix this?


tdevries

Recommended Posts

Hi, 

I am currently trying to deploy a django webapp to my website on the Tommy server. I have just completed the tutorial in the Django docs, and followed the tutorial on django app deployment on HelioNet. Currently, the webpage does load and index.html is rendered, however, the static files are not found by django. The error I get is a 404: www.mysite.com/static/App/css/bootstrap.css is not found. However the bootstrap.css is in that folder on my website, I am relatively new to this, so I don't really understand why my files can't be found...  Does anyone know what might cause this error?

Thanks in advance!

I have pasted some relevant (I think) files below and my file structure is as follows:

  • public_html/
    • Project/
      • __init__.py
      • asgi.py
      • dispatch.wsgi
      • settings.py
      • urls.py
      • wsgi.py
    • App/
      • migrations/
      • static/
        • App/
          • --bunch of static files
      • templates/
        • index.html
      • __init.py
      • admin.py
      • apps.py
      • models.py
      • tests.py
      • urls.py
      • views.py
    • static/
      • App/
        • --bunch of static files
      • admin/
        • --bunch of static files
    • manage.py
    • .htaccess

 

#index.html
{% load static %}
<!-- Bootstrap css -->
<link rel="stylesheet" href="{% static 'App/css/bootstrap.css' %}">

 

#settings.py
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-3-*qt4@^g@_$*$x483w@2)1_iw5y9e36vag$j$@zqul2zrap2e"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']

# Application definition
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "App.apps.AppConfig"
]
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "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",
]
ROOT_URLCONF = "Project.urls"
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]
WSGI_APPLICATION = "Project.wsgi.application"

# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]

# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = "static/"

STATIC_ROOT = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

 

#App/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
]

 

#Project/urls.py
from django.conf import settings
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("App/", include("App.urls")),
    path("accounts/", include("django.contrib.auth.urls")),
]

 

#views.py
from django.shortcuts import render

def index(request):
    return render(request, "App/index.html")

 

#dispatch.wsgi
import os, sys

# edit your username below
sys.path.append("/home/username_on_heliohost/public_html/")

from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'Project.settings'

application = get_wsgi_application()

 

# .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteRule ^(Project/dispatch\.wsgi/.*)$ - [L]
RewriteRule ^(.*)$ Project/dispatch.wsgi/$1 [QSA,PT,L]

 

Link to comment
Share on other sites

5 hours ago, tdevries said:

static/

5 hours ago, tdevries said:
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

In your directory structure you call the directory static, and in your .htaccess you have the directories named media and admin_media. They need to match. What RewriteRule ^(directory/.*)$ - [L] does is if the directory name matches it prevents it from being processed through your .wsgi file.

  • Thanks 1
Link to comment
Share on other sites

  • Krydos locked this topic
Guest
This topic is now closed to further replies.
×
×
  • Create New...