Tobi
Tobi

Reputation: 351

Static file cannot be found in Django view

I am having an issue with static files in the development server on Django 1.5.4. I am not sure if it is the same problem on the actual production server (running Apache), as I found a solution for that which works at the moment (simply hard coding the full URL - I know it's bad, but it gets the job done).

I am using Reportlab to create a PDF file for my project, and I need to include a picture on that. I followed the answer in a different post:

from django.templatetags.static import static
url = static('x.jpg')

Unfortunately, the answer I get from the server is an IO Error: 'Cannot open resource "localhost:8000/static/images/x.jpg"', even though a copy and paste of that into the URL bar clearly shows me that the picture is exactly there.

My settings regarding static files are the following, and they do work for everything else (CSS, Javascript, etc):

ROOT_PROJECT = os.path.join(os.path.split(__file__)[0], "..")
STATIC_ROOT = os.path.join(ROOT_PROJECT, 'static')
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

Thanks for your help!

Upvotes: 2

Views: 1160

Answers (1)

sushant-hiray
sushant-hiray

Reputation: 1898

Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

There are usually a couple of ways to store static files.

  1. One way is to create a static folder inside your app folder and store the files there. You can check that here:
  2. Is to create a folder and store your static files which are not for any particular app.

From the django documentation:

Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.

For example:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/var/www/static/',
)

If you are into production then check production deployment for more details!

Upvotes: 1

Related Questions