79183672

Date: 2024-11-13 06:46:03
Score: 1
Natty:
Report link

Hey bro don't need to panic. Let's do it.

  1. First things first you have to have the latest version of pip and here is the link to update it https://pip.pypa.io/en/stable/installation/
  2. Make below changes to your settings.py file.
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent

INSTALLED_APPS = [
    'whitenoise.runserver_nostatic', # put it at the top
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # applications are always at the bottom
    'hotdog_app',
    'corsheaders',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    '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',
    'whitenoise.middleware.WhiteNoiseMiddleware', # put it at the bottom
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  1. Run this command to automatically create the staticfiles folder and that is python manage.py collectstatic.

In staticfiles folder, you’ll find two main subfolders: an admin folder, which can be deleted if you’re not using the admin panel, and folders named after each of your apps. Within these app folders, you can add as many CSS and JavaScript files as needed. To enable static file loading, include {% load static %} at the top of your HTML file and add css link in head tag as <link rel="stylesheet" href="{% static 'test_app/css/custom.css' %}">, if you want to add js link you can do that like <script type="module" src="{% static 'test_app/js/main.js' %}"></script>.

Also, could you confirm if you're planning to deploy your application on Vercel or another platform? Most platforms follow a similar process for static file management, but it’s important to be aware of any platform-specific requirements in case issues arise. For more detailed information on configuring WhiteNoise, refer to the official documentation https://whitenoise.readthedocs.io/en/stable/django.html

Reasons:
  • Blacklisted phrase (1): here is the link
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Mohammad Shafqat Siddiqui