0

I can load static files locally with 'runserver' but not in production. I successfully manage to load templates in production, but not static files.

in the .html page I included the lines:

<!DOCTYPE html>
{% load staticfiles %} 

... ...

<img src="{% static "images/mypic.jpg" %}" alt="this pic" />

my settings in settings.py include these lines

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [STATIC_DIR, ]
STATIC_URL = '/static/'

and in urls.py I have:

from django.conf.urls.static import static

and

urlpatterns = [
    url(r'^$', views.index, name='index'), 
    url(r'^pages/', include('pages.urls')), 
    url(r'^admin/', admin.site.urls),
    url(r'about^$', views.about, name='about'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I've been trying for hours, any idea why I can't access them?

DaniPaniz
  • 1,058
  • 2
  • 13
  • 24

3 Answers3

0

(WRONG!!!)

I solved the problem by adding:

+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

to both my app urls.py and the main urls.py

DaniPaniz
  • 1,058
  • 2
  • 13
  • 24
  • 3
    The fact that this works means you have `DEBUG = True`, which is a bad idea in production. You should read up on [deploying static files](https://docs.djangoproject.com/en/1.10/howto/static-files/deployment/) and the [deployment checklist](https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/). – knbk Mar 30 '17 at 15:55
  • thanks for the suggestions, I did read [deploying static files](https://docs.djangoproject.com/en/1.10/howto/static-files/deployment/) several times, but I didn't understand s**t. And yes, I'm not a programmer, but this does not prevent me to make a website right? I'm reading several books on how to use Django, but they presuppose a lot of knowledge and they all take different approaches (which does not make things easier for newbies like me). Any further suggestions are welcome – DaniPaniz Mar 31 '17 at 04:03
  • There are lots of ways to make a website, but Django is a web framework, a tool for programmers to create websites. You can of course learn to program, but if you don't master the basics you'll have a hard time working with Django. It may help to start with learning Python. If setting up a webserver to serve static files is a daunting task, you can look into a PaaS (Platform as a Service), e.g. [Heroku](https://heroku.com), which has a free tier and excellent tutorials. – knbk Mar 31 '17 at 08:26
  • @knbk thanks a lot for your advice. Actually I do know to program with Python a little bit. I'm trying to learn Django and I'm making great progress. So I hope I'll be able to go through this process of serving static files via a webserver. – DaniPaniz Mar 31 '17 at 22:11
  • I did find a very useful post [here](http://stackoverflow.com/questions/10460028/confusion-in-django-admin-static-and-media-files/10460116#10460116) I'm gonna go through this – DaniPaniz Mar 31 '17 at 22:15
  • 1
    Great :) Another option is to use [WhiteNoise](http://whitenoise.evans.io/en/stable/) (which is what you'd use on Heroku), which is very easy to set up and suitable for production. On the other hand, it helps to know how to configure a webserver, but that's a lot more involved. – knbk Mar 31 '17 at 23:26
  • @knbk no progress so far. I'm gonna give whitenoise a try, or ask my webhost to help me out with this. p.s. is it THAT BAD to leave Debug = True in production? – DaniPaniz Apr 02 '17 at 22:50
  • 1
    @knbk WHITENOISE is just amazing!!!!! that was exactly what I needed. I finally managed to make it work, after some trials and errors, and now everything works smoothly in production with Debug = False. I don't know why don't everyone mention or suggest this. This is just brilliant :) thanks again! – DaniPaniz Apr 03 '17 at 04:27
0

In produzion sistem is un suggestable use link to file and probabily work thanks to the DEBUG=TRUE. In product environment is always better use a webserver like nginx and load the static by the webserver

Mattia
  • 961
  • 13
  • 25
  • I didn't understand what you mean. Once I set DEBUG = True (which I set False, for now, because it helps me figure out when things go wrong, and where) is my solution *not* gonna work? And why can't I use static files, if in any tutorial/book I read they don't mention this problem? p.s. my website is very simple, static and nothing can be dynamically uploaded or changed by any user. – DaniPaniz Mar 31 '17 at 04:07
  • According to Official Documentation of MEDIA: `Note This helper function works only in debug mode and only if the given prefix is local (e.g. /media/) and not a URL (e.g. http://media.example.com/).` link: [https://docs.djangoproject.com/en/1.10/howto/static-files/#serving-files-uploaded-by-a-user-during-development] For statics [https://docs.djangoproject.com/en/1.10/howto/static-files/#serving-static-files-during-development] `This is not suitable for production use! For some common deployment strategies, see Deploying static files.` Is always better read the docs that the tutorial :) – Mattia Mar 31 '17 at 07:19
  • thanks @Mattia the thing is that to me the docs are reeeeally difficult to read, because I'm not a professional programmer but a Cognitive Scientist. I happen to think that Python is an amazing programming language that can be used by scientists to do a lot of tasks, and it should be as understandable and learnable as possible. Unfortunately, the learnability issue is not always the priority among programmers... – DaniPaniz Mar 31 '17 at 22:14
  • This is why there are site like stackoverflow where people can share their knowledge. First try is always suggestable to search in docs if there are some trick to do what you need and if you need help ask of foum or on specialised websites. If you website sill on a test machine end not in prod use debug modw = true otherwise read the docs about ngunx. I only hope that my suggest is usefull for you. :) example http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html – Mattia Apr 02 '17 at 09:54
0

so, my question was Flawed. as many correctly pointed out you need to use a webserver in production to serve static files (which I didn't know as I'm not experienced).

Also, my proposed solution was very bad, because you shouldn't add the line + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The problem is that different hosting services have different ways to set this up, and often it is not possible for the user to tweak the Apache config or it is not easy to set up a nginx server. Solution:

whitenoise

Simple, beautiful, perfectly working. A few lines of codes and all your problems are solved, without having to crash your head on the wall trying to learn how to program an Apache/nginx webserver.

With whitenoise what you need to do is:

  1. install whitenoise package
  2. change your wsgi app as explained in the instructions by adding

    from whitenoise import WhiteNoise

    ...

    application = get_wsgi_application() application = WhiteNoise(application,root='/mypath/static')

  3. change the settings.py and enable whitenoise in the middleware classes ]

  4. run collectstatic

    python manage.py collectstatic

what I did not do is:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

but I kept my static_root path as: STATIC_ROOT = STATIC_DIR

I have no idea why, but this way it works

I hope this is gonna be useful to those who don't manage to serve static files in production in django

DaniPaniz
  • 1,058
  • 2
  • 13
  • 24