如何解决Django live服务器中的滞后问题?

时间:2019-09-14 17:50:47

标签: django python-3.x

我有一个小的蛋白质数据库。您可以看到代码here。假设用户从数据库中搜索了他们感兴趣的蛋白质。他们可以将序列添加到购物车。而且,他们可以使用会话将其自定义数据上传到数据库。如果用户清除会话,则其自定义数据将被删除。目的是在数据库中进行其他分析(例如成对比对)。问题是当用户想要清除会话时。清除会话后,html页面仍显示数据。约60秒后,会话将清除。我在脚本中使用匿名会话。

该数据库在本地运行良好。我不知道问题出在哪里。我为数据库尝试了SQLite3和PostgreSQL。问题仍然存在。

"""
Django settings for database project.

Generated by 'django-admin startproject' using Django 2.2.5.

For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os, socket

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*******************************************'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'database',
    'captcha',
    'crispy_forms',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'database.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join('templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'database.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "/media/"

CRISPY_TEMPLATE_PACK = 'bootstrap4'

PROJECT_DIR = os.path.dirname(__file__)

on_production_server = True if socket.gethostname() == '*****' else False

DEBUG = True if not on_production_server else False
TEMPLATE_DEBUG = DEBUG

已使用以下版本

attrs==19.1.0
biopython==1.73
captcha==0.3
certifi==2019.9.11
chardet==3.0.4
colorclass==2.2.0
dj-database-url==0.5.0
Django==2.1
django-crispy-forms==1.7.2
django-tables2==2.1.0
docopt==0.6.2
ete3==3.1.1
numpy==1.16.4
packaging==19.1
Pillow==6.1.0
pip-upgrader==1.4.15
psycopg2==2.7.6.1
psycopg2-binary==2.7.6.1
pyparsing==2.4.2
PyQt5==5.13.0
PyQt5-sip==4.19.18
pytz==2019.2
requests==2.22.0
six==1.12.0
sqlparse==0.3.0
terminaltables==3.1.0
urllib3==1.25.3
uWSGI==2.0.17.1
whitenoise==4.1.3

我使用NGINX和uWSGI在Red Hat Enterprise Linux Server 7.6(Maipo)中进行部署。数据库中的数据非常小(700个蛋白质序列)。我们部门中没有系统管理员。因此,我正在网上寻找答案。我是Web开发框架(Django)以及部署的新手。如何识别问题并排除故障?你能指出问题所在吗?

1 个答案:

答案 0 :(得分:0)

我通过编码器解决了此问题。问题不在于Django。它与nginx配置文件一起使用。添加了以下行

location / {    
         add_header 'Cache-Control' 'no-store, no-cache,
         must-revalidate, proxy-revalidate, max-age=0'; expires off; }

引荐:https://ryanclouser.com/2015/07/16/nginx-Disable-Caching/