Django - 从生产中访问请求META数据

时间:2021-05-23 11:44:08

I'm trying to send a list of datetimes to the client by JSON formatted as its locale.

我正在尝试通过JSON格式化为其语言环境向客户端发送日期时间列表。

So the main issue is actually trying to get the locale of the client.

所以主要问题实际上是试图获取客户端的语言环境。

I tried to use request.META['LC_TIME'] (which seems to be the client's prefered locale for dates and times)

我尝试使用request.META ['LC_TIME'](这似乎是客户端日期和时间的首选语言环境)

This key is here in development but not in production.

这个密钥正在开发中,但不在生产中。

KeyError: 'LC_TIME'

How can it be explained ? Am I on the right track ?

怎么解释?我是在正确的轨道上吗?

1 个解决方案

#1


1  

First, let's determine what you mean under 'Production environment'. Under DEV environment, the browser connects directly to the Django web server, and all HTTP headers are sent directly to it. In PRODUCTION, you usually have a proxy. This could be an nginx or other similar software. Their main purpose is to redirect, while performing some checks. You should check in your PRODUCTION environment, what is the actual setup of the proxy(if any), and if it strips any HTTP header sent from client(which seems like a valid reason for the error you get).

首先,让我们确定您在“生产环境”下的含义。在DEV环境下,浏览器直接连接到Django Web服务器,所有HTTP头都直接发送给它。在PRODUCTION中,您通常有代理。这可能是一个nginx或其他类似的软件。它们的主要目的是在执行某些检查时重定向。您应该检查您的PRODUCTION环境,代理的实际设置是什么(如果有的话),以及它是否剥离从客户端发送的任何HTTP头(这似乎是您获得的错误的正当理由)。

Besides the configuration issues, it is recommended to use a default value:

除配置问题外,建议使用默认值:

user_lc_time = request.META.get('LC_TIME', default_lc_time)

#1


1  

First, let's determine what you mean under 'Production environment'. Under DEV environment, the browser connects directly to the Django web server, and all HTTP headers are sent directly to it. In PRODUCTION, you usually have a proxy. This could be an nginx or other similar software. Their main purpose is to redirect, while performing some checks. You should check in your PRODUCTION environment, what is the actual setup of the proxy(if any), and if it strips any HTTP header sent from client(which seems like a valid reason for the error you get).

首先,让我们确定您在“生产环境”下的含义。在DEV环境下,浏览器直接连接到Django Web服务器,所有HTTP头都直接发送给它。在PRODUCTION中,您通常有代理。这可能是一个nginx或其他类似的软件。它们的主要目的是在执行某些检查时重定向。您应该检查您的PRODUCTION环境,代理的实际设置是什么(如果有的话),以及它是否剥离从客户端发送的任何HTTP头(这似乎是您获得的错误的正当理由)。

Besides the configuration issues, it is recommended to use a default value:

除配置问题外,建议使用默认值:

user_lc_time = request.META.get('LC_TIME', default_lc_time)