[英]Django on Apache with mod_wsgi: ImportError on settings
[英]Newbie confounded by mod_wsgi/apache/django settings
這是我關於SO的第一篇文章。 我太沮喪了,無法繼續研究它,所以我將發布設置,看看是否有人願意提供幫助。
首先,當我運行Apache作為服務(以管理員身份)時,文檔根目錄允許我通過url訪問文件(雖然不酷,但實際上不是我的問題,因為我知道這是Apache conf中的文檔根目錄設置)。 其次,當我鍵入127.0.0.1/blog/blog/時,它將引發500錯誤,並且Apache日志顯示為:
[Tue Sep 24 19:44:20 2013] [info] mod_wsgi (pid=2152): Create interpreter '127.0.0.1|/blog'.
[Tue Sep 24 19:44:20 2013] [info] mod_wsgi (pid=2152): Adding 'C:/Users/jdp/Documents/www/mysite.com' to path.
[Tue Sep 24 19:44:20 2013] [info] [client 127.0.0.1] mod_wsgi (pid=2152, process='', application='127.0.0.1|/blog'): Loading WSGI script 'C:/Users/jdp/Documents/www/mysite.com/mysite/wsgi.py'.
[Tue Sep 24 19:49:40 2013] [info] [client 127.0.0.1] (OS 10053)An established connection was aborted by the software in your host machine. : core_output_filter: writing data to the network
[Tue Sep 24 19:49:40 2013] [error] [client 127.0.0.1] mod_wsgi (pid=2152): Exception occurred processing WSGI script 'C:/Users/jdp/Documents/www/mysite.com/mysite/wsgi.py'.
[Tue Sep 24 19:49:40 2013] [error] [client 127.0.0.1] IOError: failed to write data
[Tue Sep 24 19:49:48 2013] [info] [client 127.0.0.1] (OS 10053)An established connection was aborted by the software in your host machine. : core_output_filter: writing data to the network
[Tue Sep 24 19:49:48 2013] [error] [client 127.0.0.1] mod_wsgi (pid=2152): Exception occurred processing WSGI script 'C:/Users/jdp/Documents/www/mysite.com/mysite/wsgi.py'.
[Tue Sep 24 19:49:48 2013] [error] [client 127.0.0.1] IOError: failed to write data
[Tue Sep 24 19:52:10 2013] [info] [client 127.0.0.1] (OS 10053)An established connection was aborted by the software in your host machine. : core_output_filter: writing data to the network
[Tue Sep 24 19:52:10 2013] [error] [client 127.0.0.1] mod_wsgi (pid=2152): Exception occurred processing WSGI script 'C:/Users/jdp/Documents/www/mysite.com/mysite/wsgi.py'.
[Tue Sep 24 19:52:10 2013] [error] [client 127.0.0.1] IOError: failed to write data
順便說一句,在Windows上。
首先:http.conf:
WSGIPythonPath C:/Users/jdp/Documents/www/mysite.com
<VirtualHost *:80>
ServerName 127.0.0.1
ServerAlias randomserver.com
ServerAdmin randomguy@example.com
DocumentRoot C:/Users/usr/Documents/www/
<Directory C:/Users/usr/Documents/www/>
Order allow,deny
Allow from all
</Directory>
WSGIPassAuthorization On
WSGIScriptAlias /blog C:/Users/usr/Documents/www/mysite.com/mysite/wsgi.py
<Directory C:/Users/usr/Documents/www/mysite.com/mysite>
<Files wsgi.py>
Order deny, allow
Allow from all
</Files>
</Directory>
</VirtualHost>
最初我試圖做的是:
WSGIPythonPath C:/Users/usr/Documents/www/mysite.com:C:/Users/jdp/Documents/site/env/Lib/site-packages
但這導致路徑#1出現問題,我無法導入mysite.settings。 我忽略了它,只是將缺少的軟件包下載到了我的python根目錄。
這是我在C:/Users/usr/Documents/www/mysite.com/mysite/settings.py中的設置
DEBUG = False
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_db',
'USER': 'usr',
'PASSWORD': '*******',
'HOST': '127.0.0.1',
'PORT': '0080',
}
}
ALLOWED_HOSTS = ['*']
TIME_ZONE = 'America\New_York'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = True
MEDIA_ROOT = r'C:\Users\usr\Documents\site\mysite\media'
MEDIA_URL = 'media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
SECRET_KEY = '*********************************'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
TEMPLATE_DIRS = (
'C:/Users/usr/Documents/www/mysite.com/templates',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'blog',
'south',
'django.contrib.markup',
)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
這是我的wsgi.py文件:
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
和我的urls.py:
from django.conf.urls import patterns, include, url
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls', namespace='blogs')),
)
不知道其他信息是否有用。 任何幫助將不勝感激! 我花了太多時間嘗試進行一些細微的調整,並搜索SO和mod_wsgi文檔而徒勞無功。 由於我是編程新手,所以我可能會付出太多努力,但我不認為自己已經被這樣的困擾了。
您不應將項目源代碼放在DocumentRoot下,該文檔記錄在:
這將確保您的源代碼不可訪問。
500錯誤是由於您的代碼損壞而引起的,也可能是因為您可能沒有正確設置Django以其他方式記錄錯誤的詳細信息。 您似乎正在嘗試使用日志記錄系統將其通過電子郵件發送給您錯誤消息,但是由於您尚未設置ADMINS,因此這些消息可能無法通過。
啟用調試模式可以使錯誤顯示在瀏覽器中。
FWIW,錯誤:
IOError: failed to write data
這是因為在可以寫回任何響應之前,已終止了來自瀏覽器的連接。 您可能已強制重新加載頁面或其他內容。
@Foon @Graham Dumpleton
謝謝! 一旦我更改了數據庫設置以使其與PGAdmin中的postgres設置相匹配並修復了時區錯誤,便會加載頁面。
DEBUG = False
TEMPLATE_DEBUG = DEBUG
ADMINS = (
('usr', 'usr@gmail.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_db',
'USER': 'usr',
'PASSWORD': 'blahblahblah',
'HOST': 'localhost',
'PORT': '5432',
}
}
ALLOWED_HOSTS = ['*']
TIME_ZONE = 'America/New_York'
現在,我只需要弄清楚如何在服務器上配置我的靜態文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.