Django - 来自上下文处理器的变量在部署时未更新但在本地工作
Django - Variable coming from context processor not updated when deployed but works locally
我正在使用 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()
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
}
抱歉拖了这么久 post,但我认为这些信息可能是相关的。感谢您的宝贵时间和帮助。
谢谢!
当您的 utils.py 第一次导入时,变量 catalan_text_date
... 会被计算,因为它们位于您的顶层文件。当您启动 gunicorn 时会发生这种情况,因为它会导入 context_processors.py 然后又会导入这些变量。从那时起,它再也不需要调用 cat_date()
因为 catalan_text_date
在内存中并且是全局的。
您应该在上下文处理器中调用函数 es_date()
和 cat_date()
,而不是使用计算变量。只需导入 es_date
和 cat_date
,然后在您的 last_day
函数中使用它们。
我正在使用 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()
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
}
抱歉拖了这么久 post,但我认为这些信息可能是相关的。感谢您的宝贵时间和帮助。
谢谢!
当您的 utils.py 第一次导入时,变量 catalan_text_date
... 会被计算,因为它们位于您的顶层文件。当您启动 gunicorn 时会发生这种情况,因为它会导入 context_processors.py 然后又会导入这些变量。从那时起,它再也不需要调用 cat_date()
因为 catalan_text_date
在内存中并且是全局的。
您应该在上下文处理器中调用函数 es_date()
和 cat_date()
,而不是使用计算变量。只需导入 es_date
和 cat_date
,然后在您的 last_day
函数中使用它们。