我正在使用Nginx和Gunicorn将Django网站部署到DigitalOcean Droplet。
这个想法是每个月根据当前日期更新日期。在本地可以正常运行,但是一旦部署,该值就不会更新,为了使其更新,我必须使用以下命令重新启动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()
负责将变量传递到模板的 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
}
很抱歉发布了这么长时间,但我认为这些信息可能是相关的。感谢您的时间和帮助。
谢谢!
答案 0 :(得分:0)
第一次导入 utils.py 时,将计算变量catalan_text_date
...,因为它们位于文件的顶层。当您启动gunicorn时会发生这种情况,因为它会导入 context_processors.py ,然后又会导入这些变量。从那时起,它不再需要再次调用cat_date()
,因为catalan_text_date
在内存中并且是全局的。
您应该在上下文处理器中调用函数es_date()
和cat_date()
,而不要使用计算出的变量。只需导入es_date
和cat_date
,然后在last_day
函数中使用它们即可。