【问题标题】:django fails, sometimes, very erratic behaviordjango 失败,有时,非常不稳定的行为
【发布时间】: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 脚本,这是我刷新应用程序所需的一切。

【问题讨论】:

标签: django apache2


【解决方案1】:

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

请放心,每天都有成千上万的网站可靠地使用 Django。你的问题不是 Django 特有的。

【讨论】:

  • 不,我自己没有在会话变量中存储任何内容,django.contrib.auth 模块可能但我对此表示怀疑,我有一个与用户关联的 UserProfile 类,但需要时手动检索。
  • 我不是指会话变量,我指的是模块级变量。您可能必须发布一些代码才能获得真正的答案。
【解决方案2】:

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

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

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

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

一切顺利! :)

【讨论】:

  • 测试运行良好,应用程序在开发服务器中运行良好,这是我遇到问题的生产服务器。
【解决方案3】:

如果您认为问题出在此处,则需要显示您的视图代码。否则我们只能猜测可能是什么问题。

【讨论】:

    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多