Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

Cannot make it work with DEBUG=False on Heroku #11

Open
filipeximenes opened this issue Aug 6, 2013 · 39 comments
Open

Cannot make it work with DEBUG=False on Heroku #11

filipeximenes opened this issue Aug 6, 2013 · 39 comments

Comments

@filipeximenes
Copy link

I'm trying to serve statics on heroku with DEBUG=False. When DEBUG is True everything works perfectly so it is when I run my application locally with Heroku's Foreman. Collectstatic runs with no problems in both ambients.
Forman works with the following setting:

STATIC_ROOT = root('staticfiles')
STATIC_URL = '/static/'

But i've tried setting it with:

STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

with no success.

My app is structured in the following way:

Procfile
requirements.txt
projetc/
   manage.py
   project/
   staticfiles/

I'm setting --pythonpath to the project folder in the procfile.

@kennethreitz
Copy link
Contributor

Are you using the latest release?

@filipeximenes
Copy link
Author

I'm using the one available on pip:

dj-static==0.0.5
static==0.4

@syphar
Copy link

syphar commented Aug 6, 2013

@filipeximenes
silly question, but have you tried DEBUG=False locally with foreman?

@filipeximenes
Copy link
Author

Yes.
When I have
STATIC_ROOT = root('staticfiles')
Foreman works on debug True and False

@filipeximenes
Copy link
Author

Not sure if i made some mistake or just got confused, but Forman is working perfectly with:

STATIC_ROOT = 'staticfiles'
DEBUG = False

I still can't get Heroku to serve my staticfiles.

@filipeximenes
Copy link
Author

Also, when I run collectstatic, files are being saved at the root directory (not the "django project root")

Procfile
requirements.txt
staticfiles/
projetc/
   manage.py
   project/

(this is the configuration Foreman works)

@filipeximenes
Copy link
Author

I changed the whole project to a more traditional structure:

Procfile
requirements.txt
staticfiles/
manage.py
project/
apps/
   app1/
   app2/

Still getting the same result. It works with forman but not in heroku. I destroyed my previous application in heroku and started another with no success.
One thing to notice is tha heroku is not automatically running collectstatic as it is supposet to (https://devcenter.heroku.com/articles/django-assets)
Also, every time a run collectstatic it copies all the files again.

@syphar
Copy link

syphar commented Aug 8, 2013

This does not sound like a problem with dj-static (or static), it seems more this is your app not fully working on heroku :)

When Heroku does not run collectstatic on push, the static files are missing.
When you run it with heroku run the files aren't collected and compiled into your slug (what is what you need)

@filipeximenes
Copy link
Author

Well, I really don't know where is the problem. I just made it work putting collectstatic inside Procfile:
web: python project/manage.py collectstatic --noinput; gunicorn --pythonpath project project.wsgi
this will do for now.
Still curious to know what's the problem and if it's my mistake or heroku's.

@patrick91
Copy link

I've the same problem. Maybe there's something wrong with --pythonpath project? (I'm just guessing).

I've even tried to run collect static after a push but nothing changed, the file were copied but when I try to access a static file I get a 404 error.

@filipeximenes solution partially worked for me I can see my assets but admin assets are missing.

I'm going to create a ticket on heroku support site.

@idan
Copy link

idan commented Sep 1, 2013

Also running into this issue. Strange.

@patrick91
Copy link

FYI this is the reply I received from Heroku support

Patrick,
I believe most of your issues is due to your repository layout. The custom PYTHONPATH you've provided shouldn't be necessary.
We recommend developers place the directory that contains manage.py as the root of their repository. This makes things much easier.

@filipeximenes
Copy link
Author

I have tried the "traditional" project layout with no success (see previous messages).
@patrick91 can you try this on your project and tell us if it solves the problem?

Also, just yesterday a friend of mine came to me (after finding this discussion) and told me he had the same problem, I've checked and he had manage.py in the root of his repository.

@patrick91
Copy link

@filipeximenes I'll do as soon as possible :)

@cmoad
Copy link

cmoad commented Sep 24, 2013

+1 We're seeing this only work with "DEBUG = True" as well.

@HondoOhnaka
Copy link

Just chiming in as getting this error as well.

@pawmar
Copy link

pawmar commented Nov 9, 2013

Same problem (manage.py in root). The only workaround that works for me is running collectstatic locally and adding generated staticfiles directory to git, before pushing to heroku.

@mahmoudhossam
Copy link

I'm also having the same problem, and the manage script is in the root of the repo.

@wkschwartz
Copy link

I'm having the same problem, manage.py is in the root of my repo, and pip freeze | grep static gives

dj-static==0.0.5
static==1.0.2

The problem occurs both on Heroku and locally using honcho (a python foreman clone)

@manimkv
Copy link

manimkv commented Jan 31, 2014

Mee too had this problem, i solved it.

Just try

in settings.py

DEBUG = False 

then add

DEBUG404 = True 
ALLOWED_HOSTS = ['*'] # it works but not secure, so use

ALLOWED_HOSTS = ['localhost', 'IP adrs'] #if you are running locally, then run with python manage.py runserver --insecure.You can give your webserver here.

Then in urls.py add

import os
if settings.DEBUG404:
    urlpatterns += patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': os.path.join(os.path.dirname(__file__), 'static')} ),
    )

@wkschwartz
Copy link

The problem with that solution is that then my site would still have a debug setting on. Plus, according to https://docs.djangoproject.com/en/1.6/howto/static-files/ regarding django.views.static.serve

This method is grossly inefficient and probably insecure, so it is unsuitable for production.

@gone
Copy link
Contributor

gone commented Feb 24, 2014

I had this issue - Turns out I was doing the dry run passing in a settings file, and was grabbing a default value in the wsgi file. Since the collect static runs outside wsgi, it didn't have a settings file associated, but the collect static run in a separate worker worked fine.

@hktonylee
Copy link

I found the cause. I turned on config variable DISABLE_COLLECTSTATIC=1 on heroku. Later I tried to turn it off by setting it to 0 (DISABLE_COLLECTSTATIC=0). But it actually does not work as expected. You have to totally remove this config variable on heroku.

@xag
Copy link

xag commented Jul 4, 2014

@hktonylee in my case I don't have this variable set on heroku
@pawmar your solution works for me.

  1. run collectstatic locally (> python manage.py collectstatic --noinput)
  2. remove staticfile from .gitignore, add it to git and commit
  3. push to heroku

@flevour
Copy link

flevour commented Mar 5, 2015

For anyone having this issue it would help a lot if you could setup a vanilla Django project with the bare minimum configuration to reproduce the error, thanks!

@rahmonov
Copy link

rahmonov commented Oct 5, 2015

Had the same problem. Solved it by setting ALLOWED_HOSTS to [IP ADDRESS].

@kennethreitz
Copy link
Contributor

@rahmonov I do ALLOWED_HOSTS = ['*']

@daniloroddrigues
Copy link

daniloroddrigues commented Jul 12, 2018

I tried this approach

application = Cling (MediaCling (get_wsgi_application ()))

but in heroku it does not work, only static
the media does not work.

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

MEDIA_ROOT = os.path.join (BASE_DIR, 'pypanel', 'media')
MEDIA_URL = '/media/'

@gnut-neygun
Copy link

Is this issue still not fixed? I'm having the same problem. Only debug=true let the website runs, when I turn it off, it can't read the staticfiles.

@Dipeshpal
Copy link

need help, the same problem here

@PaymasterMax
Copy link

Me two. Also my uploaded profile images disappear after a while

@Leggendario12
Copy link

Make sure to check the wsgi.py that it loads that correct DJANGO_SETTINGS_MODULE

@jedijason
Copy link

I was having the same issues. With debug=True both the vs Code/Python/Django web app and Heroku web app worked. With debug=False, neither of them worked.
The below suggestion helped fixed the issue in my case.
link

@ghost
Copy link

ghost commented Jun 25, 2020

I had this same issue, realized whitenoise was causing the issue. So I just set STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage' instead of STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage', and everything worked fine.

@Ahmed1262005
Copy link

Ahmed1262005 commented Nov 5, 2020

I tried every single suggestion in this forum till now and none did it
the whitenoise gives me a server error when debug false
serve does nothing
adding path does nothing
static_root the static works but media doesnt
debug404 does nothing
adding custom 404 url does nothing
confugiring logs to work withe heroku does nothing
DEBUG_PROPAGATE_EXCEPTIONS = True does nothing
collectstatic locally does nothing
allowedhosts=[*] changing it does nothing
changing procfile to insecure does nothing
using every other combination to procfile does nothing
disable_collastic does nothing
djano module settings are correctly loadded in wsgi.py
Cling does nothing
deleting gitgnore and upload the staticfiles to the repository with collectstatic loaclly also nothing
trying another project django-portfolio-website have the same problem and also none of the above work does any one have any other sugesstion not from the above

@jedijason
Copy link

I deployed to Heroku months ago just to see if I could take a simple blog to the next level. I'll share the links I used to solve many of my problems I had with deploying. I read in my log notes that I needed to change my code to use PostGreSQL instead of sqlite3. I also noted an issue I had were I didn't migrate my code changes.
stackoverflow
specifically the comment about referencing images that you might have commented out in your code using /* */
code band
pythonista planet
BitHubPH
RealPython

@BadLegend1609
Copy link

Mee too had this problem, i solved it.

Just try

in settings.py

DEBUG = False 

then add

DEBUG404 = True 
ALLOWED_HOSTS = ['*'] # it works but not secure, so use

ALLOWED_HOSTS = ['localhost', 'IP adrs'] #if you are running locally, then run with python manage.py runserver --insecure.You can give your webserver here.

Then in urls.py add

import os
if settings.DEBUG404:
    urlpatterns += patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': os.path.join(os.path.dirname(__file__), 'static')} ),
    )

what is patterns in the urlpatterns += patterns(....... ? it's giving me an error

@tanphuc16797
Copy link

you can using nginx then set location for it

@Ahmed1262005
Copy link

oh okay after months of failing and errors luckily I found out that it was like the most stupidest mistake ever I wrote something like /images instead of /static/images in the static root and magically it worked

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests