Я развертываю веб-сайт Django в DigitalOcean Droplet, используя Nginx и Gunicorn.
Идея состоит в том, чтобы каждый месяц обновлял дату в зависимости от текущего дня .Локально работает отлично, но после развертывания значение не обновляется, чтобы обновить его, мне нужно перезапустить Gunicorn с помощью следующей команды:
sudo systemctl restart gunicorn
Знаете ли вы, что останавливает переменныебыть обновленным?
В проекте есть приложение под названием "pages", которое utils.py отвечает за вычисление и подготовку даты на двух языках:
from babel.dates import format_date
import calendar
import datetime
import locale
import time
def cat_date():
# get current date
num_date = str(time.strftime("%d/%m/%Y"))
current_month = int(time.strftime("%m"))
current_year = int(time.strftime("%Y"))
today = datetime.date.today()
# get last day of current month
last_day_current_month = calendar.monthrange(current_year, current_month)[1]
date_last = today.replace(day=last_day_current_month)
date_last_day_current_month_num = date_last.strftime("%d/%m/%Y")
# get last week date
date_prev_week = today - datetime.timedelta(days=7)
num_date_prev_week = date_prev_week.strftime("%d/%m/%Y")
month_lw = int(date_prev_week.strftime("%m"))
year_lw = int(date_prev_week.strftime("%Y"))
# get last day of previous month
last_day_previous_month = calendar.monthrange(year_lw, month_lw)[1]
date_last_prev = date_prev_week.replace(day=last_day_previous_month)
date_last_day_previous_month_num = date_last_prev.strftime("%d/%m/%Y")
# use babel to get the long readable formated date
catalan_text_date_current = format_date(date_last, format='long', locale='ca')
catalan_text_date_previous = format_date(date_last_prev, format='long', locale='ca')
# choose date to display depending on current date
day_today = int(today.strftime("%d"))
if day_today > 3:
return date_last_day_current_month_num, catalan_text_date_current
else:
return date_last_day_previous_month_num, catalan_text_date_previous
def es_date():
# get current date
num_date = str(time.strftime("%d/%m/%Y"))
current_month = int(time.strftime("%m"))
current_year = int(time.strftime("%Y"))
today = datetime.date.today()
# get last day of current month
last_day_current_month = calendar.monthrange(current_year, current_month)[1]
date_last = today.replace(day=last_day_current_month)
date_last_day_current_month_num = date_last.strftime("%d/%m/%Y")
# get last week date
date_prev_week = today - datetime.timedelta(days=7)
num_date_prev_week = date_prev_week.strftime("%d/%m/%Y")
month_lw = int(date_prev_week.strftime("%m"))
year_lw = int(date_prev_week.strftime("%Y"))
# get last day of previous month
last_day_previous_month = calendar.monthrange(year_lw, month_lw)[1]
date_last_prev = date_prev_week.replace(day=last_day_previous_month)
date_last_day_previous_month_num = date_last_prev.strftime("%d/%m/%Y")
# use babel to get the long readable formated date
spanish_text_date_current = format_date(date_last, format='long', locale='es')
spanish_text_date_previous = format_date(date_last_prev, format='long', locale='es')
# choose date to display depending on current date
day_today = int(today.strftime("%d"))
if day_today > 3:
return date_last_day_current_month_num, spanish_text_date_current
else:
return date_last_day_previous_month_num, spanish_text_date_previous
date_last_day_num, spanish_text_date = es_date()
date_last_day_num, catalan_text_date = cat_date()
A context_processors.py , отвечающий за передачу переменных в шаблоны: *
from .utils import date_last_day_num, catalan_text_date, spanish_text_date
def last_day(request):
context = {
'date_last_day_num': date_last_day_num,
'catalan_text_date': catalan_text_date,
'spanish_text_date': spanish_text_date,
}
return context
Затем я просто вызываю переменную из шаблона:
{{ spanish_text_date }}
РЕДАКТИРОВАТЬ:
Nginx -> файл my-site_project:
# Expires map
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
server_name 123.123.123.12 my-site.com www.my-site.com;
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.geo+json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/bmp
image/svg+xml
image/x-icon
text/cache-manifest
text/css
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;
expires $expires;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/my-site/src;
}
location /media/ {
root /home/user/my-site/src;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/my-site.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/my-site.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.my-site.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = my-site.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name my-site.com www.my-site.com;
return 404; # managed by Certbot
}
Извините за такой длинный пост, но я думаю, что информацияможет быть актуальным.Я ценю ваше время и помощь.
Спасибо!