TemplateDoesNotExist only happen on prod ec2 but Templates works on local - python

I have running on my windows pc a django app with following structure
this app also runs locally and works with this settings
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_mysql',
'users',
'posts',
'comments',
]
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',
# 'chango2.middelware.ProfileCompletionMiddelware',
]
libraries: {
'tags': 'tags',
}
ROOT_URLCONF = 'chango2.urls'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
this app work as expected when its runned locally with python manage.py runserver
now i trying to deploy this app on a ec2 , i have configurated the security gropus to allow port 80 **and **8000 for inbound and outbound
on ec2 i run with
(env) ubuntu#ip-172-31-3-242:~$ gunicorn --bind 0.0.0.0:8000 chango2.wsgi:application
app starts listening on port 8000 got the first page but for other link
then i recive the
the only thing changed between prod and local was
ALLOWED_HOSTS = [my ec2 dns]
and DATABASES (but that its working)

Related

Celery results not updating after dockerization

I'm currently putting my Django app on Docker. I successfully dockerized Django, gunicorn, nginx and Celery, however when I run a Celery task, even though it's executed (displayed in the logs), the results are not stored to my database.
That worked before dockerizing everything so that probably comes from my docker configurations, but I didn't manage to find which part was incorrect/incomplete.
Also, I'm using the default sqlite3 Django database as I don't need to store a huge amount of data.
Here is my docker-compose.yml:
version: '3.8'
services:
django_gunicorn:
volumes:
- db:/db.sqlite3
- static:/static
- media:/media
env_file:
- env
build:
context: .
ports:
- "8000:8000"
command: sh -c "python manage.py migrate && python manage.py collectstatic --no-input && gunicorn main.wsgi:application --bind 0.0.0.0:8000"
nginx:
build: ./nginx
volumes:
- static:/static
- media:/media
ports:
- "80:80"
depends_on:
- django_gunicorn
rabbitmq3:
image: rabbitmq:3-alpine
ports:
- 5672:5672
celery:
restart: always
build:
context: .
command: celery -A main worker -P eventlet -c 100 -l INFO
env_file:
- env
depends_on:
- rabbitmq3
- django_gunicorn
volumes:
- db:/db.sqlite3
volumes:
db:
static:
media:
Dockerfile
FROM python:3.10.5-alpine
ENV PYTHONUNBEFFERED = 1
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./src /app
WORKDIR /app
celery.py
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
app = Celery('main', backend='django-db')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
#app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
Maybe it's because I'm using sqlite3 ? I saw everyone using PostgreSQL during my search for a solution. Should I switch to a PostgreSQL ? Or else, what should I change in order to get my tasks results in my database ?
EDIT : settings.py
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG = os.getenv('DEBUG')
# SECURITY WARNING: don't run with debug turned on in production!
#DEBUG = True
ALLOWED_HOSTS = ["127.0.0.1"]
#CSRF_COOKIE_SECURE = True
#SESSION_COOKIE_SECURE = True
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_celery_results',
'django_celery_beat',
'crispy_forms',
'crispy_bootstrap5',
'fontawesomefree',
'bootstrap_datepicker_plus',
'scripts.apps.ScriptsConfig',
'django_cleanup.apps.CleanupConfig',
]
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 = 'main.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 = 'main.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',
}
}
# 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_ROOT = BASE_DIR / 'static'
STATIC_URL = '/static/'
# Media files (scripts, results)
MEDIA_ROOT = BASE_DIR
MEDIA_URL = 'scripts/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Crispy with Bootstrap
CRISPY_TEMPLATE_PACK = 'bootstrap5'
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CELERY_BROKER_URL="amqp://guest:guest#rabbitmq3:5672/"

I got error when I turn DEBUG true on settings file

I got Error500 when I turn DEBUG True on settings.py but everything is okay when DEBUG is False. python manage.py collectstatic is okay too.
Here my settings.py. I will appreciated your help. I use Django 2.2 version.
Django settings
"""
Django settings for samamarche project.
Generated by 'django-admin startproject' using Django 2.2.
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 dj_database_url
import os
import django_heroku
if os.environ.get('ENV') == 'PRODUCTION':
# ...
db_from_env = dj_database_url.config(conn_max_age=500)
#DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
DATABASES['default'].update(db_from_env)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = os.environ.get('SECRET_KEY', 'xxxxxxxxxxxxxxx')
DEBUG = True
# 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',
'phonenumber_field',
'marche',
'cart',
'commandes',
'payment',
'coupons',
#'accounts',
'crispy_forms',
'users',
]
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'
]
i think adding ALLOWED_HOSTS = ['*'] will solve the problem

How do I allow my django website to be hosted by a local server (127.0.0.1) and a Heruko server

I don't know if I am explaining my problem correctly but I can clarify with the following picture of my settings.py file
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True
DEBUG = False
# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['sheryar-portfolio.herokuapp.com', '127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
'calculator',
'amino'
]
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',
]
Now here in ALLOWED_HOSTS, I have declared two hosts.
The Heroku one is running perfectly whereas I am having some issues in the local one.
It seems like that my static folder is not being located by django as I can only see the HTML of my website in local server.
In short, I cannot see the CSS being applied to my website when I am including the local server in ALLOWED_HOST

manage.py runserver fails after putting custom app in middleware

Im following a guide on youtube to work with django. Unfortunately this guide is made for pre 2.0 django.
So the challenge is to create a app called "posts" that can be accessed by localhost/posts.
After creating the folder 'posts' and adding it to settings.py MIDDLEWARE like this:
MIDDLEWARE = [
'posts',
'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',
]
python manage.py runserver fails
When commenting 'posts' out runserver succedes.
The problem is that Im so new to all of this that I don't even know what to search for.
try adding 'post' in INSTALLED_APPS inside settings.py like this:
INSTALLED_APPS = [
'post',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
and delete it from middleware.
You have to "install" all the apps you make

Django message error

The following error is being thrown by my Django code on my VPS when I try to use messages:
You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
Exception Type: MessageFailure
Exception Value:
You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
I do have all the appropriate settings in my settings.py file. My VPS is running Django 1.9.6. The same error is not thrown when I run the code locally on my laptop. On my laptop, I have Django 1.10.3 running. Are there any differences in the treatment of messages between Django 1.9.6 and 1.10.3 that would cause this to happen? It seems that on Django 1.9.6 my settings in settings.py are being ignored.
Relevant code in settings.py is as follows:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]
Any help would be greatly appreciated. Thanks.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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',
]
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',
],
},
},
]
follow the code with the order of each

Categories

Resources