0

I have weird question, but can anyone know what happened?

I have made Django app and everything worked fine, and then restarted server and start again Django with command:

And then static files do not loaded (CSS, images ...)... I get 404 error. What happened??? Please help... Maybe I have to run it on the other way? I really do not know what happened, because everything worked fine, and then after I started that again everything go wrong..

nohup python manage.py runserver 0.0.0.0:8000 &

This is the command that I used to start django (server) again..

Questw
  • 43
  • 4
  • Did you change from `DEBUG=True` to `Debug=False` by any chance? – dfundako Oct 16 '20 at 19:40
  • I did it few months before, but it worked fine ... – Questw Oct 16 '20 at 19:40
  • Thanks man, I put again DEBUG = TRUE and it worked... Thanks a lot.. – Questw Oct 16 '20 at 19:44
  • Can you explain me what happened, because it worked fine and what is for DEBUG = TRUE... – Questw Oct 16 '20 at 19:45
  • in short, `DEBUG=True` will do your static file handling for you since its meant for dev work. `DEBUG=False` is meant for production, but you need to handle your own static assets. https://docs.djangoproject.com/en/3.1/howto/static-files/deployment/ – dfundako Oct 16 '20 at 19:48

2 Answers2

2

First, use python manage.py runserver.
Then, check if DEBUG is True or False. Make sure it is Debug=True.
Lastly, see if you accidentally deleted any static files.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
samarmohan
  • 350
  • 3
  • 12
  • DEBUG = TRUE I changed that. But I have question why it worked fine when DEBUG = FALSE... And what is for DEBUG = TRUE important.. I am new to django, sorry.. Thanks. – Questw Oct 16 '20 at 19:49
  • So, ```DEBUG=True``` is when you are testing and not in production. If you set ``DEBUG`` to false, you need to add an allowed host in ``Allowed Hosts``. I have no clue whatsoever why the static files are disappearing, for that, you should do some research yourself. Look [here](https://stackoverflow.com/questions/25676453/what-are-functional-differences-between-debug-true-and-false-in-django#:~:text=Basically%2C%20in%20DEBUG%20mode%2C%20django,ALLOWED_HOSTS%20needs%20to%20be%20set.&text=The%20DEBUG%3DTrue%20%2C%20if%20there,will%20show%20details%20of%20error.). Anyways, I'm happy I helped! – samarmohan Oct 16 '20 at 19:53
  • Sorry, but I do not understand.. What I have to add in allowed host? – Questw Oct 16 '20 at 19:54
0

See Serve Files During Development

If you use django.contrib.staticfiles, runserver will serve staticfiles automatically when DEBUG is set to True.

You can inspect django/staticfile/views.py to see the view that serves the staticfiles. in case of not settings.DEBUG, it will raise Http404

def serve(request, path, insecure=False, **kwargs):
    """
    Serve static files below a given point in the directory structure or
    from locations inferred from the staticfiles finders.
    To use, put a URL pattern such as::
        from django.contrib.staticfiles import views
        path('', views.serve)
    in your URLconf.
    It uses the django.views.static.serve() view to serve the found files.
    """
    if not settings.DEBUG and not insecure:
        raise Http404
    ...
    
    return static.serve(request, path, document_root=document_root, **kwargs)
minglyu
  • 2,958
  • 2
  • 13
  • 32