microsoft / python-sample-vscode-django-tutorial

Sample code for the Django tutorial in the VS Code documentation

Home Page:https://code.visualstudio.com/docs/python/tutorial-django

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When building with debug = false css is lost

pandawankenobi opened this issue · comments

@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?

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.

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\ '

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.

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".

Great, thanks for the clarification!

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.