在部署在 pythonanywhere 上的 django 项目中链接静态文件时出错
Error in linking static files in django project deployed on pythonanywhere
我在 pythonanywhere 中部署了一个 django(1.7) 项目,它的 link 是- http://drchitradhawle.pythonanywhere.com/ 我猜静态文件 linking 和 [=29= 有问题] 任何人帮助我解决问题还告诉我 link 任何静态文件和 bootstrap 在 django 项目中的整个过程。
我的项目结构是-
home
-DrChitraDhawle
-website
-webpage
-static
-css
-images
-templates
-website
-settings.py
settings.py 文件为 -
STATIC_ROOT = "/home/DrChitraDhawle/website/webpage/static"
STATIC_URL = '/static/'
STATICFILES_DIR = (
('assets', '/home/DrChitraDhawle/website/webpage'),
)
#
#STATICFILES_DIR = [os.path.join(BASE_DIR, '')]
Link of static file set in web tab is -
And i have used it in my program as -
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'webpage/bootstrap.min.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'webpage/style.css' %}" />
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dr.Chitra Webpage</title>
.
.
.
error log on python anywhere is -
2015-03-08 16:09:52,766 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2015-03-08 16:09:53,926 :Traceback (most recent call last):
2015-03-08 16:09:53,926 : File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2015-03-08 16:09:53,926 : self.error_log_file.logger.exception("Error running WSGI application")
2015-03-08 16:09:53,926 : File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-03-08 16:09:53,926 : self.error(msg, *args, **kwargs)
2015-03-08 16:09:53,927 : File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-03-08 16:09:53,927 : self._log(ERROR, msg, args, **kwargs)
2015-03-08 16:09:53,927 : File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-03-08 16:09:53,928 : record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-03-08 16:09:53,928 : File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-03-08 16:09:53,928 : rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-03-08 16:09:53,928 : File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-03-08 16:09:53,928 : self.threadName = threading.current_thread().name
2015-03-08 16:09:53,928 : File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-03-08 16:09:53,929 : return _active[_get_ident()]
2015-03-08 16:09:53,929 : File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2015-03-08 16:09:53,929 : app_iterator = self.app(environ, start_response)
2015-03-08 16:09:53,929 : File "/bin/user_wsgi_wrapper.py", line 136, in import_error_application
2015-03-08 16:09:53,929 : raise e
2015-03-08 16:09:53,929 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
Access log -
78.12.109.13 - - [09/Mar/2015:10:55:39 +0000] "GET /static/webpage/images/chitraMam.jpg HTTP/1.1" 404 1165 "http://drchitradhawle.pythonanywhere.com/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:40 +0000] "GET /favicon.ico HTTP/1.1" 404 1150 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:43 +0000] "GET /static/webpage/bootstrap.min.css HTTP/1.1" 404 1164 "http://drchitradhawle.pythonanywhere.com/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:43 +0000] "GET /static/webpage/style.css HTTP/1.1" 404 1154 "http://drchitradhawle.pythonanywhere.com/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:56 +0000] "GET /static HTTP/1.1" 404 1148 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:58 +0000] "GET /static/ HTTP/1.1" 404 1149 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:56:03 +0000] "GET /static/webpage/ HTTP/1.1" 404 1151 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
14.139.240.226 - - [09/Mar/2015:11:04:16 +0000] "GET /webpage/research/ HTTP/1.1" 200 1615 "http://drchitradhawle.pythonanywhere.com/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
14.139.240.226 - - [09/Mar/2015:11:04:17 +0000] "GET /static/webpage/bootstrap.min.css HTTP/1.1" 404 1164 "http://drchitradhawle.pythonanywhere.com/webpage/research/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
14.139.240.226 - - [09/Mar/2015:11:04:17 +0000] "GET /static/webpage/style.css HTTP/1.1" 404 1154 "http://drchitradhawle.pythonanywhere.com/webpage/research/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
14.139.240.226 - - [09/Mar/2015:11:04:18 +0000] "GET /static/webpage/style.css HTTP/1.1" 404 1154 "http://drchitradhawle.pythonanywhere.com/webpage/research/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
14.139.240.226 - - [09/Mar/2015:11:04:18 +0000] "GET /static/webpage/bootstrap.min.css HTTP/1.1" 404 1164 "http://drchitradhawle.pythonanywhere.com/webpage/research/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
我会研究三件事。
您的错误日志显示:“SECRET_KEY 设置不能为空”——您应该调查这是否仍然是一个问题。在 settings.py.
中寻找 SECRET_KEY
在settings.py中,您真正需要的仅有两个设置是STATIC_URL
和STATIC_ROOT
。 STATIC_ROOT
应该是一个与您的应用程序文件夹分开的文件夹,所以我会把它放在 /home/DrChitraDhawle/website/ 之类的地方。我建议删除 STATICFILES_DIR
设置。
然后您将需要 运行 collectstatic
命令,它告诉 django 将每个应用程序中的所有静态文件复制到您的 STATIC_ROOT
文件夹中:
python manage.py collectstatic
这会将您的静态文件从 webpage/static 文件夹复制到 website/static 文件夹,并且您需要在添加或删除静态文件时重新运行它。
这里有关于 PythonAnywhere wiki 的更多信息:https://www.pythonanywhere.com/wiki/DjangoStaticFiles and in the django documentation, here: https://docs.djangoproject.com/en/1.7/howto/static-files/
你的静态文件不起作用的原因是因为你的静态文件在webtab截图中设置为home/DrChitraDhawle/website/webpage/static
。应该是/home/DrChitraDhawle/website/webpage/static
错误日志是旧的(即自上次修复旧错误后您可能没有生成任何新错误)
我在 pythonanywhere 中部署了一个 django(1.7) 项目,它的 link 是- http://drchitradhawle.pythonanywhere.com/ 我猜静态文件 linking 和 [=29= 有问题] 任何人帮助我解决问题还告诉我 link 任何静态文件和 bootstrap 在 django 项目中的整个过程。
我的项目结构是-
home
-DrChitraDhawle
-website
-webpage
-static
-css
-images
-templates
-website
-settings.py
settings.py 文件为 -
STATIC_ROOT = "/home/DrChitraDhawle/website/webpage/static"
STATIC_URL = '/static/'
STATICFILES_DIR = (
('assets', '/home/DrChitraDhawle/website/webpage'),
)
#
#STATICFILES_DIR = [os.path.join(BASE_DIR, '')]
Link of static file set in web tab is -
And i have used it in my program as -
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'webpage/bootstrap.min.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'webpage/style.css' %}" />
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dr.Chitra Webpage</title>
.
.
.
error log on python anywhere is -
2015-03-08 16:09:52,766 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2015-03-08 16:09:53,926 :Traceback (most recent call last):
2015-03-08 16:09:53,926 : File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2015-03-08 16:09:53,926 : self.error_log_file.logger.exception("Error running WSGI application")
2015-03-08 16:09:53,926 : File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-03-08 16:09:53,926 : self.error(msg, *args, **kwargs)
2015-03-08 16:09:53,927 : File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-03-08 16:09:53,927 : self._log(ERROR, msg, args, **kwargs)
2015-03-08 16:09:53,927 : File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-03-08 16:09:53,928 : record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-03-08 16:09:53,928 : File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-03-08 16:09:53,928 : rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-03-08 16:09:53,928 : File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-03-08 16:09:53,928 : self.threadName = threading.current_thread().name
2015-03-08 16:09:53,928 : File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-03-08 16:09:53,929 : return _active[_get_ident()]
2015-03-08 16:09:53,929 : File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2015-03-08 16:09:53,929 : app_iterator = self.app(environ, start_response)
2015-03-08 16:09:53,929 : File "/bin/user_wsgi_wrapper.py", line 136, in import_error_application
2015-03-08 16:09:53,929 : raise e
2015-03-08 16:09:53,929 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
Access log -
78.12.109.13 - - [09/Mar/2015:10:55:39 +0000] "GET /static/webpage/images/chitraMam.jpg HTTP/1.1" 404 1165 "http://drchitradhawle.pythonanywhere.com/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:40 +0000] "GET /favicon.ico HTTP/1.1" 404 1150 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:43 +0000] "GET /static/webpage/bootstrap.min.css HTTP/1.1" 404 1164 "http://drchitradhawle.pythonanywhere.com/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:43 +0000] "GET /static/webpage/style.css HTTP/1.1" 404 1154 "http://drchitradhawle.pythonanywhere.com/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:56 +0000] "GET /static HTTP/1.1" 404 1148 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:55:58 +0000] "GET /static/ HTTP/1.1" 404 1149 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
78.12.109.13 - - [09/Mar/2015:10:56:03 +0000] "GET /static/webpage/ HTTP/1.1" 404 1151 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36" "78.12.109.13"
14.139.240.226 - - [09/Mar/2015:11:04:16 +0000] "GET /webpage/research/ HTTP/1.1" 200 1615 "http://drchitradhawle.pythonanywhere.com/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
14.139.240.226 - - [09/Mar/2015:11:04:17 +0000] "GET /static/webpage/bootstrap.min.css HTTP/1.1" 404 1164 "http://drchitradhawle.pythonanywhere.com/webpage/research/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
14.139.240.226 - - [09/Mar/2015:11:04:17 +0000] "GET /static/webpage/style.css HTTP/1.1" 404 1154 "http://drchitradhawle.pythonanywhere.com/webpage/research/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
14.139.240.226 - - [09/Mar/2015:11:04:18 +0000] "GET /static/webpage/style.css HTTP/1.1" 404 1154 "http://drchitradhawle.pythonanywhere.com/webpage/research/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
14.139.240.226 - - [09/Mar/2015:11:04:18 +0000] "GET /static/webpage/bootstrap.min.css HTTP/1.1" 404 1164 "http://drchitradhawle.pythonanywhere.com/webpage/research/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" "14.139.240.226"
我会研究三件事。
您的错误日志显示:“SECRET_KEY 设置不能为空”——您应该调查这是否仍然是一个问题。在 settings.py.
中寻找 在settings.py中,您真正需要的仅有两个设置是
STATIC_URL
和STATIC_ROOT
。STATIC_ROOT
应该是一个与您的应用程序文件夹分开的文件夹,所以我会把它放在 /home/DrChitraDhawle/website/ 之类的地方。我建议删除STATICFILES_DIR
设置。
SECRET_KEY
然后您将需要 运行 collectstatic
命令,它告诉 django 将每个应用程序中的所有静态文件复制到您的 STATIC_ROOT
文件夹中:
python manage.py collectstatic
这会将您的静态文件从 webpage/static 文件夹复制到 website/static 文件夹,并且您需要在添加或删除静态文件时重新运行它。
这里有关于 PythonAnywhere wiki 的更多信息:https://www.pythonanywhere.com/wiki/DjangoStaticFiles and in the django documentation, here: https://docs.djangoproject.com/en/1.7/howto/static-files/
你的静态文件不起作用的原因是因为你的静态文件在webtab截图中设置为home/DrChitraDhawle/website/webpage/static
。应该是/home/DrChitraDhawle/website/webpage/static
错误日志是旧的(即自上次修复旧错误后您可能没有生成任何新错误)