8

Got my Django project on a Azure webapp, but when I call on SSH terminal:

Python manage.py collectstatic

It says 252 files copied but my static files are not visible on my templates and static folder in wwwroot its empty...Here's my wwwroot structure:

wwwroot
|---Myproject
|---manage.py
|---oryx-manifest.toml
|---hostingstart.html
|---static //With all my static files
├── myapp
│   ├── migrations
│   ├── __pycache__
│   ├── static
|   |   |---Images
|   |   |   |--myimage.png
|   |   |   |--myimage2.png
│   └── templates

And this is my settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
   ('myapp', os.path.join(BASE_DIR, 'myapp', 'static')),
)
STATICFILES_FINDERS = (
  'django.contrib.staticfiles.finders.FileSystemFinder',
  'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

Any idea why or what am I doing wrong ?, does Azure collect different ?

EDIT> When I go to my website my images don´t show...Im calling them like this on template:

{% load static %}
<img src="{% static 'Images/myimage.png' %}" /><br>

EDIT 2 /////

In wwwroot creates indeed a folder with all my statics, but when I load my template they don´t show, in wen console I get this error for myimage.png and myimage2.png :

Failed to load resource: the server responded with a status of 404 (Not Found)
jsanchezs
  • 1,992
  • 3
  • 25
  • 51

4 Answers4

13

Found it !!

Just had to add this: + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to url patterns like this:

from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
        path('myapp/', include('myapp.urls')),
        path('admin/', admin.site.urls),
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

And it did the trick, hope it helps to anyone else!!

SharePointer
  • 169
  • 9
jsanchezs
  • 1,992
  • 3
  • 25
  • 51
8

Django does not serve static files in production mode.

The only time it serves static files is when you turn DEBUG = True which is in development mode and this is not recommended in a production setting.

Django recommends to serve static files via CND or any other webserver. However if your website is minimal and does not get high volume traffic you can serve your static files using Django whitenoise and here is how you do it.

The below solution works on python 3.7 and Django 3.2

Step 1: Install whitenoise package

pip install whitenoise

Step 2: Ensure your BASE_DIR looks like the below

BASE_DIR = Path(__file__).resolve().parent.parent

Step 3: Add these to your settings.py. If you get any errors try commenting out STATICFILES_STORAGE and check

STATIC_ROOT = BASE_DIR / 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Step 4: 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',
  # ...
]

Step 5: Ensure this is how you reference static files in your templates(also check if you have {% load static %} mentioned in your template

<link rel="stylesheet" type="text/css" href="{% static 'appname/styles.css' %}">

Step 6: Run collect static

python manage.py collectstatic

Step 7: Turn DEBUG = False and run the server to verify it works.

Some additional resources to read further:

whitenoise
Django on Azure - beyond "hello world"

Neil
  • 175
  • 1
  • 8
1

Few points to note:

Below line will change

STATIC_ROOT = (os.path.join(BASE_DIR, 'Myproject/static_files/'))

to

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

You are using pointing to completely different folder here. Hence always empty You need the collectstatic command to copy files to Project > static directory


Below remove the line os.path.join(BASE_DIR, 'static/')...

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'), #Not Required since you already mentioned it in STATIC_URL
('myapp', os.path.join(BASE_DIR, 'myapp', 'static')),
)

Reason:

  • STATIC_ROOT is the folder where static files will be stored after using manage.py collectstatic

The absolute path to the directory where collectstatic will collect static files for deployment.

If the staticfiles contrib app is enabled (default) the collectstatic management command will collect static files into this directory. See the howto on managing static files for more details about usage.

  • STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.

This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.

If issue still persists check your static file directory in apache mod_wsgi config file in the server if it is configured properly.

Check this link for help on that >> https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/modwsgi/


Cheers!!!

  • Thanks for your kind reply. Did what you told me but still don´t get statics, I mean, in wwwroot collectstatics indeed creates a folder with everything, but when I load my page they don´t show and in web console shows : Failed to load resource: the server responded with a status of 404 (Not Found)... – jsanchezs Jan 20 '21 at 16:35
  • Could you share your deployment configurations for your django app in Azure? How & where have you pointed your static folder in your config? – Ayan Banerjee Jan 27 '21 at 12:51
  • Great. Good luck mate! :) – Ayan Banerjee Jan 28 '21 at 07:46
  • @jsanchezs This only works if you turn on debugging mode and stops working as soon as you turn off debugging which can't be used for production. Did it get it to work while DEBUG = False? – Neil Aug 02 '21 at 22:19
0

I would recommend checking out Whitenoise for serving your static files with Django. I haven't had an issue serving them since integrating Whitenoise. Try swapping it out with your current static file finders setup.

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

twrought
  • 636
  • 5
  • 9