簡體   English   中英

當 Debug = False 時,Django 站點無法找到管理樣式或媒體文件

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

我在使用 debug = False 運行我的 django 站點時遇到問題。

信息:

  • 目前,Debug = False
  • 我在本地主機上運行
  • 我正在使用 Wagtail。 但是從我讀到的內容來看,我正在處理的問題更多地與 Django 相關。

問題:

  • 當我在本地主機上訪問該站點時,它可以找到靜態文件,但無法獲取任何“媒體”文件。例如: GET http://127.0.0.1:8000/media/images/block-alphabet-abc.width-400.png Internal Server Error

  • 當我登錄管理面板時,某些地方的外觀和感覺是關閉的(例如 css 組件可能丟失)。

  • 當我去編輯一個頁面時,外觀和感覺仍然關閉,Web 開發人員工具有一堆 500 錯誤,比如http://127.0.0.1:8000/static/wagtailadmin/js/date-time-chooser.js?v=3a1ab45f丟失。

我嘗試過的:

  • 我已經運行了python manage.py collectstatic 這樣做不會創建媒體目錄。 它添加了 wagtail 管理文件,但最后都沒有小哈希。
  • 我嘗試運行 python manage.py compress,但我不斷收到錯誤,提示沒有任何模板具有壓縮標簽(這是真的)。
  • 我添加了日志記錄,但我不確定如何解決這些錯誤。

題:
如何創建媒體目錄? 如何以正確的格式添加 wagtail 管理文件以便可以找到它們?

settings.py 中的靜態和媒體 URL

# 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/'

使用 STATIC_URL 的 Base.html 模板的頂部:

{% 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>

日志:
這些日志顯示了當我在管理面板中編輯頁面時會發生什么。

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'

這些日志顯示了當我轉到前端站點頁面時會發生什么。

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

Django 的 runserver 不能用於生產。 通過將DEBUG設置為True ,您現在可以在類似生產的環境中運行。 runserver 不會提供媒體文件,所以如果你想在開發機器上模仿服務器風格的設置,你應該做的是安裝一個生產類型的服務器,比如 Apache 並將其設置為像往常一樣提供媒體文件使用正確的別名。 您還需要為靜態文件創建別名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM