Roman Rdgz
Roman Rdgz

Reputation: 13244

My Django app is not serving static files

This is quite strange. It was working OK, but the machine it was running broke and I had to mount it again without success. The webapp shows with wuite background, without images, css nor js loaded.

This is how the app is structured: App folder structure

These are the variables in settings.py which matter (please ask for any other if required):

DEBUG = True
TEMPLATE_DEBUG = DEBUG

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    '/var/www/myapp/static/',
)

And into my tempaltes, I always set the contents' URL like this:

<img id="logo" src="{{STATIC_URL}}images/logo.png"\>

Any idea about where the problem is?

Upvotes: 0

Views: 96

Answers (5)

Roman Rdgz
Roman Rdgz

Reputation: 13244

I finally discovered the error: Apache was running as default and serving the html templates in port 80, but without permissions for the static files.

Upvotes: 0

user2657983
user2657983

Reputation:

The settings you have are generally discouraged, for local development they will work fine, however if you were to attempt to run that on a Linux based machine, it wouldn't work.

Here's how it should be formatted:

Here are the settings that would make it like that:

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')
#this applies to all operating systems
STATICFILES_DIRS = (
 os.path.join(BASE_DIR, 'static', 'static_dirs'),
)

You would put your static folders and files in "static_dirs", and then whenever you needed to sync to "static_root". You would type: "python manage.py collectstatic".

In your template file you would then type:

{% load static from staticfiles %}

<img id="logo" src="{% static "images/logo.png" %}"\>

That should work.

Upvotes: 1

Jon Combe
Jon Combe

Reputation: 176

You comment in reply to the post by Allen Fernandes above indicates you are in DEBUG mode. Is django.contrib.staticfiles in your INSTALLED_APPS?

Upvotes: 0

Hungry Mind
Hungry Mind

Reputation: 226

Include {% load staticfiles %} in template.

Upvotes: 0

Allen Fernandes
Allen Fernandes

Reputation: 1353

check if the source is loaded using view source and if yes from there check if any link to your static assets are accessible if present then you have to hard refresh the browser

Upvotes: 0

Related Questions