JustBlossom
JustBlossom

Reputation: 1329

Django Site Can't Find Admin Styling or Media Files when Debug = False

I'm having trouble getting my django site running with debug = False.

Info:

Problem:

What I have tried:

Question:
How do I create the media directory? How do I get the wagtail admin files added in the right format so they can be found?

STATIC & MEDIA URLs in settings.py

# Static files (CSS, JavaScript, Images)

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, 'static'),
]

# ManifestStaticFilesStorage is recommended in production, to prevent outdated
# Javascript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

STATIC_ROOT = "/var/www/html/static/"
STATIC_URL = '/static/'

MEDIA_ROOT = "/var/www/html/media/"
MEDIA_URL = '/media/'

Top of the Base.html Template that use STATIC_URL:

{% load static wagtailuserbar %}
{% load wagtailcore_tags %}
{% load wagtailcore_tags site_tags %}
{% load wagtailcore_tags wagtailimages_tags %}

<!DOCTYPE html>
<html class="no-js" lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">


        <title>
            {% block title %}
            {% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}
            {% endblock %}
            {% block title_suffix %}
            {% with self.get_site.site_name as site_name %}
            {% if site_name %} | {{ site_name }}{% endif %}
            {% endwith %}
            {% endblock %}
        </title>


        <meta name="description" content="" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link href="{{ STATIC_URL }} /static/bootstrap-3.4.1-dist/css/bootstrap.css" rel="stylesheet" type="text/css"/>
        <link href="{{ STATIC_URL }} /static/bootstrap-3.4.1-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="{{ STATIC_URL }} /static/jquery-3.3.1.min.js"></script>
        <script type="text/javascript" src="{{ STATIC_URL }} /static/bootstrap-3.4.1-dist/js/bootstrap.min.js"></script>

    {% get_global_settings request=request as global_settings %}
    {% if global_settings.site_logo %} 
        {% image global_settings.site_logo width-2000 as site_logo %}
        <link rel="icon" href="{{ site_logo.url }}" type="image/png" sizes="16x16">
    {% endif %}

        {# Global stylesheets #}
        <link rel="stylesheet" type="text/css" href="{% static 'css/sitename.css' %}">

        {% block extra_css %}
        {# Override this in templates to add extra stylesheets #}
        {% endblock %}

    </head>

Logs:
These logs show what happens when I go to edit a page in the admin panel.

Exception while resolving variable 'field_classes' in template 'wagtailadmin/edit_handlers/single_field_panel.html'.
Traceback (most recent call last):
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/template/base.py", line 829, in _resolve_lookup
    current = current[bit]
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/template/context.py", line 83, in __getitem__
    raise KeyError(key)
KeyError: 'field_classes'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/template/base.py", line 835, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'Context' has no attribute 'field_classes'

These logs show what happens when I go to a front-end site page.

Exception while resolving variable 'STATIC_URL' in template 'home/home_page.html'.
Traceback (most recent call last):
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/template/base.py", line 829, in _resolve_lookup
    current = current[bit]
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/template/context.py", line 83, in __getitem__
    raise KeyError(key)
KeyError: 'STATIC_URL'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/template/base.py", line 835, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'RequestContext' has no attribute 'STATIC_URL'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/template/base.py", line 843, in _resolve_lookup
    current = current[int(bit)]
ValueError: invalid literal for int() with base 10: 'STATIC_URL'

...

Traceback (most recent call last):
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/template/base.py", line 850, in _resolve_lookup
    (bit, current))  # missing attribute
django.template.base.VariableDoesNotExist: Failed lookup for key [STATIC_URL] in [{'True': True, 'None': None, 'False': False}, {'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'ERROR': 40, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30}, 'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x7efe01e64598>>, 'request': <WSGIRequest: GET '/media/images/twitter-logo.width-40.png'>, 'user': <SimpleLazyObject: <function AuthenticationMiddleware.process_request.<locals>.<lambda> at 0x7efe01e64400>>, 'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7efe01e65128>, 'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7efe01e509e8>}, {}, {'request_path': '/media/images/twitter-logo.width-40.png', 'exception': '"/var/www/html/media/images/twitter-logo.width-40.png" does not exist'}]
Exception while resolving variable 'page' in template '404.html'.
Traceback (most recent call last):
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/<local-directory-to-app>/env/lib/python3.5/site-packages/django/views/static.py", line 42, in serve
    raise Http404(_('"%(path)s" does not exist') % {'path': fullpath})
django.http.response.Http404: "/var/www/html/media/images/walking-in-tunnel.width-2000.png" does not exist

Upvotes: 0

Views: 1012

Answers (1)

Dan Swain
Dan Swain

Reputation: 3098

Django's runserver is not to be used in production. By setting DEBUG to True, you are now running in a production-like environment. runserver won't serve media files, so if you want to mimic a server-style setup on a dev machine, what you should do is install a production-type server such as Apache and set it up to serve media files as you normally would with the proper alias. You would also need to create an alias for static files.

Upvotes: 1

Related Questions