简体   繁体   中英

Serving Static File on Django

I have been trying to serve my static files at my Django project, however it cannot find or load it. I have tried different approaches, but none seems to fix the issue.

The static folder is in the same directory as the manage.py.

Also, I have installed the WitheNoise but it also did not solve the problem.

In addition: I am using docker, I have done the collect static and checked the container. All the files are correctly there.

Django version = 2.0.1

Development environment

Code Structure:

Project

- assets
- config
- docs
- project-root
- - static
- - manage.py
- - templates
- - apps
- - project-root
- - - settings.py
- - - urls.py
...
...

setting.py

INSTALLED_APPS = [
    'pages.apps.PagesConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

html file

{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <!-- Bootstrap -->
  <link rel="stylesheet" href="{% static "css/bootstrap.css" %}">
  <link rel="stylesheet" href="{% static "css/jquery.bxslider.css" %}">
  <!-- Custom -->
  <link rel="stylesheet" href="{% static "css/style.css" %}">
  <link rel="stylesheet" href="{% static "css/animate.css" %}">

</head>

Let me know if there is anything else that I need to add to the post that will help you. Thank you,

The version of Django is not mentioned in the question, also the environment - (production/development)

-In recent versions of python {% load static %} is recommended instead of {% load staticfiles %}

-If Debug is True and if django.contrib.staticfiles is not present in INSTALLED_APP -

-either add django.contrib.staticfiles to INSTALLED_APP or append static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to urlpatterns to serve the static files.

  • Also for static files not in app folders the directory needs to be listed in STATICFILES_DIRS-
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    'any_other_locations',
]

Edit your settings.py file and add WhiteNoise to the MIDDLEWARE list. The WhiteNoise middleware should be placed directly after the Django SecurityMiddleware (if you are using it) and before all other middleware.

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  #(Rest of the Middleware here)
]

Routes for the whitenoise cache and static files:

STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

And run the collectstatic

django-admin collectstatic

or

python3 manage.py collecstatic

If you want to run in inside the docker container, here is more info

https://docs.docker.com/engine/reference/commandline/exec/

More info about Whitenoise:

http://whitenoise.evans.io/en/stable/django.html#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM