【发布时间】:2009-12-15 00:30:05
【问题描述】:
我在 django 无缘无故地对相同请求做出不同响应时遇到了可怕的问题。
有时我刷新,我看到一个错误,然后我刷新它消失了,有时模板抱怨缺少值,然后它再次正常,再次没有解释。
有时它从生产服务器中提取图形,有时从开发服务器中提取图形。
最糟糕的是,有时,很多时候,一切看起来和加载都很好,但是一个查询返回 0 次命中,甚至基于相同查询加载的第二个查询集也很困难,同一个查询如何设法失败 部分 在同一个请求中?对于最后一个,我的理论是它根本没有从视图中获取查询集变量。
我在谷歌搜索这个问题时不知所措,我正处于将项目移植到 php 的边缘,因为至少它总是呈现相同的。
该项目是使用 apache2 作为 wsgi 安装的,是的,每次更新时我都会确保使用 WSGI 脚本 touch wsgi.py。
请帮忙。
apache 的虚拟主机配置:
<VirtualHost *>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/self/example.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/self/example.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Alias /media/ /home/self/projects/django/myapp/media/
<Directory /home/self/projects/django/myapp/media/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /home/self/projects/django/myapp/wsgi.py
<Directory /home/self/projects/django/myapp/>
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/self/example.com/error.log
LogLevel warn
CustomLog /home/self/example.com/access.log combined
ServerSignature Off
</VirtualHost>
这是 wsgi.py
import os
import sys
sys.path.append('/home/self/projects/django')
sys.path.append('/home/self/projects/django/myapp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
还有settings.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Django settings for myapp project.
import os
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('My self', 'self@example.com'),
)
MANAGERS = ADMINS
DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = os.path.join(PROJECT_DIR, 'myapp.db') # Or path to database file if using sqlite3.
DATABASE_USER = '' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Tegucigalpa'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'es-es'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = 'http://media.example.com/media/'
AUTH_PROFILE_MODULE = "myapp.userprofile"
LOGIN_URL = "/login"
# Make this unique, and don't share it with anybody.
SECRET_KEY = '1(!u3tvq^=x)y@kny&^eg&uevo6&y%k-wgl$q$-sl_0+s%3g^5'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'pagination.middleware.PaginationMiddleware',
)
ROOT_URLCONF = 'myapp.urls'
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR, 'templates'),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'myapp',
'pagination',
)
更新:
现在我很肯定,模板可以正常工作并正确更新,即视图行为不规律但有规律,它们有时会表现出旧行为。
这听起来像是一个缓存问题,但我自己并没有做任何缓存,而且我总是使用touch wsgi.py 来更新 WSGI 脚本,这是我刷新应用程序所需的一切。
【问题讨论】:
-
听起来有点像my question