[Debug = False时,Django站点找不到管理员样式或媒体文件

问题描述 投票:0回答:1

我在使我的django网站无法通过debug = False运行时遇到麻烦。

Info:

  • 当前,调试= False
  • 我正在本地主机上运行
  • 我正在使用Wa。但是从我阅读的内容来看,我正在处理的问题更多是与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,但是我不断收到以下错误消息:所有模板都没有压缩标签(是真的)。​​
  • 我添加了日志记录,但是不确定如何解决错误。

问题:如何创建媒体目录?如何获取以正确格式添加的added管理文件,以便可以找到它们?

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网站运行debug = False时遇到麻烦。信息:当前,Debug = False我正在使用Wagtail在本地主机上运行。但是从我所读的书中,我遇到的问题是...

python django wagtail
1个回答
1
投票

Django的runserver不能在生产中使用。通过将DEBUG设置为True,您现在可以在生产环境中运行。 runserver won't serve media files,因此,如果要模拟开发机上的服务器样式设置,则应该做的是安装生产类型的服务器(例如Apache),并将其设置为以正常方式提供媒体文件,别名。您还需要为静态文件创建别名。

© www.soinside.com 2019 - 2024. All rights reserved.