Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When building with debug = false css is lost #13

Closed
pandawankenobi opened this issue Nov 26, 2018 · 7 comments
Closed

When building with debug = false css is lost #13

pandawankenobi opened this issue Nov 26, 2018 · 7 comments

Comments

@pandawankenobi
Copy link

pandawankenobi commented Nov 26, 2018

@kraigb
Static files don't seem to be served when debug = false.

To replicate in vs code + win 10:

  1. Git clone
  2. remove python==3.7 from requirements (wont build otherwise)
  3. Docker build
  4. Docker run locally
  5. Everything fine
  6. Set debug=False
  7. Docker build
  8. Docker run locally
  9. remember to ctrl+f5 refresh page.
    No CSS loading. It seems static files are missing...

I tried pushing to webapp with same result. Am i missing something?

@kraigb
Copy link
Contributor

kraigb commented Dec 5, 2018

Thanks; I'll look into it when I have a chance (which might not be until after the holidays. In the meantime, it sounds like https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail, which is a little confusing because the docker image is set up to use a production server instead of the Django dev server. Anyway, let me know if the SO thread helps.

@pandawankenobi
Copy link
Author

With a lot of help @jfroejk the issue has been resolved. We suggest adding the following at the end of the dockerfile:

run echo > /etc/nginx/conf.d/nginx.conf $'server {\n\ listen 8000;\n\ location / {\n\ include uwsgi_params;\n\ uwsgi_pass unix:///tmp/uwsgi.sock;\n\ }\n\ location ^~ /static {\n\ alias /app/staticfiles;\n\ }\n\ }\n\ '

@pandawankenobi
Copy link
Author

Dockerfile.txt

@kraigb
Copy link
Contributor

kraigb commented Jan 14, 2019

Thanks again for this. I'm making a note in the sample's readme.md, and will also add a note to the VS Code tutorial pointing to this issue. For the sake of keeping things simple, though, I'm not adding the extra line to the main sample or doc, given that it would take quite a bit of explaining that would otherwise distract from the tutorial's purpose.

@kraigb kraigb closed this as completed in a1ae2bb Jan 14, 2019
@pandawankenobi
Copy link
Author

No problem. Just an update: It's not actually that the CSS is lost, it's that the static files are not served at all. At first i thought it was css, but the problem was that i used https://pypi.org/project/django-material/ to provide layout/styles. Django material relies on static files. However CDN's work just fine. So generally: Anything depending on static files will fail when debug = false. I suppose one could check for static files by running "manage.py collectstatic".

@kraigb
Copy link
Contributor

kraigb commented Jan 21, 2019

Great, thanks for the clarification!

@imba-tjd
Copy link

imba-tjd commented Mar 24, 2023

For

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

If you read the sourcecode

# https://github.com/django/django/blob/main/django/contrib/staticfiles/urls.py
def staticfiles_urlpatterns(prefix=None):
    """
    Helper function to return a URL pattern for serving static files.
    """
    if prefix is None:
        prefix = settings.STATIC_URL
    return static(prefix, view=serve)


# https://github.com/django/django/blob/main/django/conf/urls/static.py
def static(prefix, view=serve, **kwargs):
    """
    Return a URL pattern for serving files in debug mode.
    from django.conf import settings
    from django.conf.urls.static import static
    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    """
    if not prefix:
        raise ImproperlyConfigured("Empty static prefix not permitted")
    elif not settings.DEBUG or urlsplit(prefix).netloc:
        # No-op if not in debug mode or a non-local prefix.
        return []
    return [
        re_path(
            r"^%s(?P<path>.*)$" % re.escape(prefix.lstrip("/")), view, kwargs=kwargs
        ),
    ]

It returns empty list in Debug=False.
In Debug=True, you don't need it, because runserver handles it.
It's useful only when Debug=True and not using runserver.

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

No branches or pull requests

3 participants