I am developing a django project using nginx on the production. But when I start running my nginx server, the django_session table on the mysql starts growing. In each resfresh, I see more rows in the table. Why is that happening? Any way to stop this?
Here is my django settings.
import os
import platform
from pathlib import Path
from mydjangoapp import utilities as util
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'my secret key' #deidentified
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'example.com']
CSRF_TRUSTED_ORIGINS = ['http://localhost', 'http://example.com', 'https://example.com']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'mydjangoapp',
'corsheaders',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'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 = 'mydjangoproj.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'mydjangoapp/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 = 'mydjangoproj.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
'default': {},
'db_magicnote': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb_test',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
DATABASE_ROUTERS = ['mydjangoapp.dbRouter.MyDbRouter']
CURRENT_DATABASE = 'db_mydjangoapp'
# Password validation
# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = '/static/'
#STATICFILES_DIRS = [
# BASE_DIR / "static",
#]
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
# MEDIA
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
LOGIN_REDIRECT_URL = '/'
### SESSION SETTINGS ###
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
And here is my nginx conf settings:
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name example.com;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias C:/Users/Administrator/Documents/mydjangoproj/media; # your Django project's media files - amend as required
}
location /static {
alias C:/Users/Administrator/Documents/mydjangoproj/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
proxy_pass http://localhost:8080;
}
}
Okay, I've finally figured it out. This has nothing to do with the nginx web server. The health check of my AWS application load balancer was sending request every 5 seconds to my website.
I applied two steps:
1-) I increased the interval value to 300 seconds and timeout value to 120 seconds on the health check configuration (these are their maximum values). This way they don't make frequent request to the server that would increase the size of the django_sessions table significantly.
2-) I've started clearing expired sessions in the django_sessions table. There are a couple of ways to handle that:
a-) On MySQL, you can manually run the following sql:
DELETE FROM mydatabase.django_session where expire_date<now()
b-) Under the Django's main project directory, you can manually run the following command on your terminal:
python manage.py clearsessions
c-) One of these commands can be put in a shell script and scheduled to be run periodically (let's say once every day).
I prefer the python command (item 2) for now. As my userbase grows and the project gets larger, I can think of automating this manual process (the last item).
Related
I'm hosting my Django application on a VPS and I'm using Django Rest Framework and the Django Admin Site.
Everything seemed working running fine (check the below image),
but when I try to click on my API's I'm redirected to a Server Error (500) page.
One thing to notice is that when I'm running the app locally on my machine everything is working fine.
Below is my settings.py if it can help in anyway:
Django settings for thepatron project.
Generated by 'django-admin startproject' using Django 3.2.4.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
import os
import django_heroku
import datetime
from dotenv import load_dotenv
# Initialize environment variables
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
try:
SECRET_KEY = str(os.getenv('SECRET_KEY'))
except KeyError as e:
raise RuntimeError("Could not find a SECRET_KEY in environment") from e
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = [
# VPS
'194.5.159.80',
# the-patron
'the-patron.com'
# Heroku
'https://the-patron-backend.herokuapp.com/',
# Hostinger IP Address
'141.136.43.2',
# Home IP Address
'127.0.0.1',
# Local Frontend Port
'localhost:4200',
]
AUTH_USER_MODEL = 'store.User'
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
# CORS_ORIGIN_ALLOW_ALL = False
# CORS_ORIGIN_WHITELIST = (
# 'http//:localhost:8000',
# )
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'django_filters',
'store',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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',
'django.contrib.auth.middleware.RemoteUserMiddleware',
'corsheaders.middleware.CorsMiddleware',
]
CORS_ALLOW_ALL_ORIGINS = True # If this is used then `CORS_ALLOWED_ORIGINS` will not have any effect
CORS_ALLOW_CREDENTIALS = True
ROOT_URLCONF = 'thepatron.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, '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 = 'thepatron.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
# Database settings to connect to hosted database
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': str(os.getenv('DATABASE_NAME')),
'USER': str(os.getenv('DATABASE_USER')),
'PASSWORD': str(os.getenv('DATABASE_PASS')),
'HOST': '141.136.43.2',
'PORT': '3306',
'OPTIONS': {
'sql_mode': 'traditional',
}
}
}
# Password validation
# https://docs.djangoproject.com/en/3.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/3.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/3.2/howto/static-files/
if DEBUG:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'store/static/store'),
os.path.join(BASE_DIR, 'staticfiles/restframework'),
]
else:
# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# URL to use when referring to static files located in STATIC_ROOT.
STATIC_URL = '/static/'
# MEDIA_ROOT is for server path to store files in the computer.
# This is where the backend loads media like images from
MEDIA_ROOT = os.path.join(BASE_DIR, 'staticfiles/store/media')
# MEDIA_URL is the reference URL for browser to access the files over Http.
MEDIA_URL = '/media/'
STATICFILES_STORAGE = [
'django.contrib.staticfiles.storage.StaticFilesStorage',
]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
'rest_framework.filters.OrderingFilter',
],
'DEFAULT_AUTHENTICATION_CLASSES':
('rest_framework_simplejwt.authentication.JWTAuthentication', )
}
JWT_AUTH = {
'JWT_ALLOW_REFRESH': True,
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3600),
}
# Activate Django-Heroku.
django_heroku.settings(locals())
This works for me
In settings.py
DEBUG = True
I just started learning Django framework, and I tried to deploy my first project on the server from DigitalOcean. If I run
python3 manage.py runserver 0.0.0.0:8000
The server launches. However, once I try to access it from (my-rent-ip):8000 I get this:
[25/Aug/2021 01:49:01] code 400, message Bad request syntax ('\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03GÛî}ز\x921\x0e\x94\x17\x9cÏe¹\x88ñÿÃ\x16\x01éÖRÝ\x00\x95F\x8aG\tÉ 8¯,úÂ\x93´ù\x06Ý\x14¾z\x13Âe4[\x9a,.æ\x96+$<~\x8eq<´\t\x00"ZZ\x13\x01\x13\x02\x13\x03À+À/À,À0̨̩À\x13À\x14\x00\x9c\x00\x9d\x00/\x005\x00')
[25/Aug/2021 01:49:01] You're accessing the development server over HTTPS, but it only supports HTTP
How is this possible if I run production server, not a development one?
I might be doing something wrong with the setting.py since it had to be changed a lot for the production purposes. I made a production branch, changed settings.py file, and cloned to the server using GitHub. Here it is:
from pathlib import Path
from dotenv import load_dotenv #for python-dotenv method
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
import os
SECRET_KEY = os.environ.get('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
# DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
]
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 = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'mysite.wsgi.application'
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.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/3.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/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
'/var/www/static/',
]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Thank you so much!
Your server does not support https, so set these settings:
SECURE_SSL_REDIRECT=False
SESSION_COOKIE_SECURE=False
CSRF_COOKIE_SECURE=False
Probably now you need to clear the caches of your browser and restart your server.
The error message refers to the development server because python3 manage.py runserver is used only in development.
Note that you should set up https in your production server (when you will have set up https sets again settings that we changed earlier to True) and that setting ALLOWED_HOSTS = ['*'] is a security issue.
Use http instead of https in url
I had the same problem I was able to access it using http on ec2 ubuntu 20.04
In settings.py use ALLOWED_HOSTS = ['*' ,'18.11.22.58']
Make sure you change ec2 security group and allow traffic to port 8000
Run server like this python3 manage.py runserver 0.0.0.0:8000
Try accesing your site as http://18.11.22.58:8000/ (Not https)
To see how to run on https see here : https://stackoverflow.com/a/51384868/3904109
I deployed my django app with Heroku and I am having troubles with connecting to Postgres.
If I run python manage.py createsuperuser after running heroku ps:exec --app produceit (so I am inside the Heroku instance's terminal), it says that I successfully created a superuser but nothing is written into Heroku Postgress DB...so I can't login into neither /admin nor my app.
I therefore run
heroku ps:exec --app produceit
python manage.py shell
from mywebsite.settings import DATABASES
and it seems like the settings.py has the variable DATABASE still connected to sqlite (see pic)
Funny enough, if I hardcode an admin into Heroku's Postgres DB, loging and then create an instance of one of my models from the app itself (like I create a blog's post)...it actually writes into Postgres and I see it on the Front End.
So basically it's like the app is working on Postgress BUT the terminal is running on SQLite.
What can I do? My settings are below
"""
Django settings for mywebsite project.
Generated by 'django-admin startproject' using Django 3.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.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
DEBUG_PROPAGATE_EXCEPTIONS = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'produceit.herokuapp.com']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'action.apps.ActionConfig',
'django_extensions',
'bootstrap_modal_forms',
'widget_tweaks',
]
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',
#'whitenoise.middleware.WhiteNoiseMiddleware'
]
ROOT_URLCONF = 'mywebsite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'mywebsite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
#DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': '',
# 'HOST': '',
# 'PORT': 5432,
# 'USER': '',
# 'PASSWORD': '',
# }
# }
# Parse database configuration from $DATABASE_URL
#import dj_database_url
#db_from_env = dj_database_url.config(conn_max_age=500)
#DATABASES['default'].update(db_from_env)
# Password validation
# https://docs.djangoproject.com/en/3.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/3.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/3.2/howto/static-files/
# Static files (CSS, JavaScript, Images)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
#DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
import django_on_heroku
django_on_heroku.settings(locals())
#SECURE_SSL_REDIRECT=True
#SESSION_COOKIE_SECURE=True
#CSRF_COOKIE_SECURE=True
#SECURE_HSTS_SECONDS = 31536000
#SECURE_HSTS_INCLUDE_SUBDOMAINS=True
#SECURE_HSTS_PRELOAD=True
#import django_heroku
#django_heroku.settings(locals())
To get the correct environment variables etc., connect to the terminal via the Heroku dashboard and try it again. You can find it in "More" > "Run Console" > Type in "Bash" on your Heroku-app.
I am trying to build an image in django that will later be deployed on a digitalocean droplet with my own domain name. I am currently trying to get rid of an issue that I believe is affecting my progress in relation to my local postgis container. I have a container named: postgis-container in the network: awm. After I run:
python manage.py makemigrations
I get the error:
django.db.utils.OperationalError: could not translate host name "postgis-container" to address: Temporary failure in name resolution
I was told by my lecturer to use the network alias for the ALLOWED_HOSTS field in my settings.py but it didn't make any difference. I put a comment to the right hand side of the possible offending line.
settings.py
"""
Django settings for AdvancedWebMapping project.
Generated by 'django-admin startproject' using Django 3.1.3.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
import socket
from pathlib import Path
import docker_config
from whitenoise.storage import CompressedManifestStaticFilesStorage
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = docker_config.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',
'django.contrib.gis',
'world',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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 = 'AdvancedWebMapping.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / '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 = 'AdvancedWebMapping.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'gis',
'HOST': 'localhost',
'USER': 'docker',
'PASSWORD': 'docker',
'PORT': '25432',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/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/3.1/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/3.1/howto/static-files/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"
if socket.gethostname() =="matthew#acer":
DATABASES["default"]["HOST"] = "localhost"
DATABASES["default"]["PORT"] = 25432
else:
DATABASES["default"]["HOST"] = "postgis-container" # offending line?
DATABASES["default"]["PORT"] = 5432
# Set DEPLOY_SECURE to True only for LIVE deployment
if docker_config.DEPLOY_SECURE:
DEBUG = False
TEMPLATES[0]["OPTIONS"]["debug"] = False
ALLOWED_HOSTS = ['.matthewmawm.xyz', 'localhost', '209.97.133.19']
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
CORS_ORIGIN_ALLOW_ALL = True
else:
DEBUG = True
TEMPLATES[0]["OPTIONS"]["debug"] = True
ALLOWED_HOSTS = ['*', ]
CSRF_COOKIE_SECURE = False
SESSION_COOKIE_SECURE = False
how I created the postgis container (docker):
sudo docker create --name postgis-container --network awm --network-alias postgis-container -t -p 25432:5432 -v name_of_volume:/var/lib/postgresql kartoza/postgis
My own mistake. My hostname was incorrect at socket.gethostname().
I have a small protein database. You can see the codes here. Say a user searches for their proteins of interest from the database. They can add the sequences to the cart. Also, they can upload their custom data to the database using session. If the user clear the session their custom data will be removed. The purpose is to do additional analysis in the database (say pairwise alignments). The problem is when a user wants to clear session. After clearing the session still the html page shows the data. After ~60 seconds the session clears. I use anonymous sessions in the script.
The database works perfectly well locally. I couldn't figure out where the problem is. I tried both SQLite3 as well as PostgreSQL for the database. Still the problem persists.
"""
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
The following versions are used installed
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
I use NGINX and uWSGI for deployment in the Red Hat Enterprise Linux Server release 7.6 (Maipo). The data in the database is very small (700 protein sequences). There are no system admins in our department. Hence I am looking for the answer online. I am new to web development framework (Django) as well as deploying. How to identify and troubleshoot the problem? Can you point me where the problem could be?
The problem is not with Django. It's with nginx configuration file. Added the below line
location / {
add_header 'Cache-Control' 'no-store, no-cache,
must-revalidate, proxy-revalidate, max-age=0'; expires off; }
Referred: https://ryanclouser.com/2015/07/16/nginx-Disable-Caching/