我无法在django网站上运行debug = False。
信息:
问题:
当我在本地主机上访问该站点时,它可以找到静态文件,但无法获取任何“媒体”文件。例如: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管理文件,但最后没有一个散列符。 问题:
如何创建媒体目录?如何获得以正确格式添加的w管理文件,以便可以找到它们?
静态和媒体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
答案 0 :(得分:1)
Django的runserver不能在生产中使用。通过将DEBUG
设置为True
,您现在可以在生产环境中运行。 runserver won't serve media files,因此,如果要模拟开发计算机上的服务器样式设置,则应该安装一个生产类型的服务器(例如Apache),并将其设置为像平时那样处理媒体文件适当的别名。您还需要为静态文件创建别名。