django失败,有时,非常不稳定的行为

时间:2009-12-15 00:30:06

标签: django apache2

我有一些可怕的问题,django对相同的请求做出不同的反应,没有明显的理由。

有时候我会刷新并且看到错误,然后我刷新并且它消失了,有时候模板会再次抱怨缺失值然后再次罚款,再次没有解释。

有时它会从生产服务器中提取图形,有时从开发服务器中提取图形。

最糟糕的是,有时候,很多时候,一切看起来和加载都很好,但查询返回0次点击甚至很难第二个查询集基于相同的查询加载正常,同一个查询如何设置失败部分< / em>在同一个请求中?对于最后一个,我有理论认为它根本没有从视图中获取queryset变量。

我在谷歌搜索关于这个问题的关键字丢失,我正处于将项目移植到php的边缘,因为至少它总是呈现相同的。

该项目使用apache2作为wsgi安装,每次更新时都是,我确保touch wsgi.py,即WSGI脚本。

请帮忙

apache的vhost配置:

<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脚本,这是刷新应用程序所需的一切。

3 个答案:

答案 0 :(得分:2)

您是否将状态存储在全局变量中?如果是这样,响应将根据先前对同一过程的请求而有所不同。

请放心,Django每天都被数以千计的网站可靠地使用。你的问题不是Django特有的。

答案 1 :(得分:1)

不幸的是,这听起来好像你遇到了常见的问题超载。当有太多错误的东西时,很难弄清楚到底发生了什么。

当它变得复杂时,有时最好的解决方案是遵循KISS开发方法。编写一小段代码,并测试它们以确保它们按预期工作。

如果您担心WSGI,请先运行一个小型的简单项目。然后引入复杂的层次以尝试并隔离问题开始发生的地方;提供了解决错误的好方法。

利用python unittest.TestCase模块和doctests来帮助你进行错误检查的python方面。

一切顺利! :)

答案 2 :(得分:0)

如果您认为问题存在,则需要显示您的观看代码。否则我们只能猜测问题可能是什么。