0

I am using angular heroku and django. I have been noticing that when I use 'ng build' to build new static files to add to django then push to heroku, the heroku instance is showing a website that is several versions behind what my current code is.

I attempted to run my django local server today after doing ngbuild placing my files in the designated folder,

running python manage.py collectstatic

which it runs successfully.

then I run my django server, navigate to my page and I get a 500 response.

Because I am using angular, I have set up my django server as a rest backend.

every endpoint the rest service uses starts with the url api/

so localdomain/api/ <-- restful service

localdomain alone serves the angular app.

I only receive the 500 sever error when I try and get the app served.

Here are all my settings regarding static files:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',

#angular distro root
ANGULAR_APP_DIR = os.path.join(BASE_DIR, 'frontend/dist/')
#image distro root
ASSETS_DIR = os.path.join(BASE_DIR, 'frontend/dist/assets/')


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
#STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(ANGULAR_APP_DIR),
    os.path.join(ASSETS_DIR),

]

my template settings:

ROOT_URLCONF = 'suitsandtables.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['suitsandtables/templates',
                 'stemail/templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

my url to serve static files and my url to render the index.html page

url(r'^(?!/?static/)(?!/?media/)(?P<path>.*\..*)$',
        RedirectView.as_view(url='/static/%(path)s', permanent=False)),
 url(r'^$', views.RootView.as_view()),

my rootview class to render the index.html

class RootView(TemplateView):
    def get(self, request, **kwargs):
        return render(request, 'index.html', context = None)
weridpotato
  • 261
  • 1
  • 3
  • 11

1 Answers1

6

This is a common problem with whitenoise. I have wasted countless hours on this. Heres how you can deal with this issue.

put these two lines in your settings.py

DEBUG = False
DEBUG_PROPAGATE_EXCEPTIONS = True

and try to run the app on heroku. It will break just like it always did.

Now go to

https://dashboard.heroku.com/apps/<app_name>/logs

Normally you wont be able to see logs when debug is False, but thanks to DEBUG_PROPAGATE_EXCEPTIONS=True you can see logs

There, look for the file which whitenoise can't find and making whitenoise go haywire. In my case it was some random css file which was reference in my base.html.

either you can fix its location or simply delete the line from base.html which references this file.

after this, if tehre is issue with another file(s) keep doing it.

Eventually whitenoise will be happy, so will you be.

Ojas Kale
  • 2,067
  • 2
  • 24
  • 39
  • thank you, I will check this out, and give you the gooey points if it works! Appreciate you!! – weridpotato Jun 29 '18 at 19:16
  • but to be clear, my heroku app is not breaking by showing a 500 issue, rather it is not displaying the new version of the site. Does your fix still apply? – weridpotato Jun 29 '18 at 19:29
  • 500 is internal server error, if its showing its breaking. what do you min by not breaking ? – Ojas Kale Jun 29 '18 at 19:40
  • the 500 internal server error is only showing on my django local it is not showing on heroku. Or maybe heroku is just bypassing that and using an older version? Nevermind I think I am overthinking it – weridpotato Jun 29 '18 at 19:43
  • well if it ever break on prod because of whitenoise, you have a solution :) – Ojas Kale Jun 29 '18 at 19:47
  • so i did what you said and yep I got an error: `raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name) ValueError: Missing staticfiles manifest entry for 'inline.bundle.js'` – weridpotato Jun 29 '18 at 19:49
  • Okay look for the line in your html that includes `inline.bundle.js`. comment that line out, you should see the error go away. if it works try to fix the location fo that file., – Ojas Kale Jun 29 '18 at 19:52
  • im not sure what you mean but try and fix the location of that file. This is an angular project on the front end. has this anything to do with the base href attribute? – weridpotato Jun 29 '18 at 21:14
  • This is what I know, if whitenoise could not find a file it breaks straight up. In the past I have fixed similar issues like this. When I said fix the location of file, I meant Look for the reason why whitenoise is not able to find the file. May be your static root and url for prod are not right. May be file is not there, where whitenoise is looking. Something along this line. – Ojas Kale Jun 29 '18 at 21:20