如何设置嵌入在特殊非 http URL (Python/Django) 中的 URL 的缓存控制
How to set Cache-Control of a URL embedded within a special non-http URL (Python/Django)
背景:
在我的 Django view
的 form_valid
方法中,我正在通过 字符串连接 构造一个 URL。假设上述 URL 是 http://example.com/uuid,其中 uuid
是传递给此 form_valid
方法的 POST 变量。
在这个方法中,接下来我将构建一个自定义 HttpResponse
对象,它将导致 非 http url,即 nonhttp_url = "sms:"+phonenumber+"?body="+body
. body
包含一些文本和我之前通过字符串连接形成的 url(例如 'go to this url: [url]
')。 phonenumber
是任何合法的手机号码。
这样构建的HttpResponse
对象实际上是一个HTML技巧,用于打开phone的原生短信应用程序并预先填充phone ] 号码和短信正文。它的常见用法是 <a href="sms:phonenumber?body="+body">Send SMS</a>
。我实际上是在调用相同的东西,但是是从我的 Django 视图的 form_valid
方法内部调用的。
问题:
如何确保我通过上面的 str 连接形成并传递到非 http url 的 body
部分的 URL 设置为 Cache-Control
no-cache
?其实我也想要no-store
和must-revalidate
。同样,我也需要Pragma
设置为no-cache
,Expires
设置为0
,Vary
设置为*
。
当前代码:
class UserPhoneNumberView(FormView):
form_class = UserPhoneNumberForm
template_name = "get_user_phonenumber.html"
def form_valid(self, form):
phonenumber = self.request.POST.get("mobile_number")
unique = self.request.POST.get("unique")
url = "http://example.com/"+unique
response = HttpResponse("", status=302)
body = "See this url: "+url
nonhttp_url = "sms:"+phonenumber+"?body="+body
response['Location'] = nonhttp_url
return response
我认为您会像对待 HTTP URL 那样做?您可以将 header 设置为 using the response as a dictionary,就像设置 Location
header 一样。在这种情况下,添加如下行:response['Vary'] = '*'
.
为方便起见,您可以使用 add_never_cache_headers()
添加 Cache-Control
和 Expires
header。
这是一个例子:
from django.utils.cache import add_never_cache_headers
class UserPhoneNumberView(FormView):
form_class = UserPhoneNumberForm
template_name = "get_user_phonenumber.html"
def form_valid(self, form):
phonenumber = self.request.POST.get("mobile_number")
unique = self.request.POST.get("unique")
url = "http://example.com/"+unique
response = HttpResponse("", status=302)
body = "See this url: "+url
nonhttp_url = "sms:"+phonenumber+"?body="+body
response['Location'] = nonhttp_url
# This will add the proper Cache-Control and Expires
add_never_cache_headers(response)
# Now just add the 'Vary' header
response['Vary'] = '*'
return response
背景:
在我的 Django view
的 form_valid
方法中,我正在通过 字符串连接 构造一个 URL。假设上述 URL 是 http://example.com/uuid,其中 uuid
是传递给此 form_valid
方法的 POST 变量。
在这个方法中,接下来我将构建一个自定义 HttpResponse
对象,它将导致 非 http url,即 nonhttp_url = "sms:"+phonenumber+"?body="+body
. body
包含一些文本和我之前通过字符串连接形成的 url(例如 'go to this url: [url]
')。 phonenumber
是任何合法的手机号码。
这样构建的HttpResponse
对象实际上是一个HTML技巧,用于打开phone的原生短信应用程序并预先填充phone ] 号码和短信正文。它的常见用法是 <a href="sms:phonenumber?body="+body">Send SMS</a>
。我实际上是在调用相同的东西,但是是从我的 Django 视图的 form_valid
方法内部调用的。
问题:
如何确保我通过上面的 str 连接形成并传递到非 http url 的 body
部分的 URL 设置为 Cache-Control
no-cache
?其实我也想要no-store
和must-revalidate
。同样,我也需要Pragma
设置为no-cache
,Expires
设置为0
,Vary
设置为*
。
当前代码:
class UserPhoneNumberView(FormView):
form_class = UserPhoneNumberForm
template_name = "get_user_phonenumber.html"
def form_valid(self, form):
phonenumber = self.request.POST.get("mobile_number")
unique = self.request.POST.get("unique")
url = "http://example.com/"+unique
response = HttpResponse("", status=302)
body = "See this url: "+url
nonhttp_url = "sms:"+phonenumber+"?body="+body
response['Location'] = nonhttp_url
return response
我认为您会像对待 HTTP URL 那样做?您可以将 header 设置为 using the response as a dictionary,就像设置 Location
header 一样。在这种情况下,添加如下行:response['Vary'] = '*'
.
为方便起见,您可以使用 add_never_cache_headers()
添加 Cache-Control
和 Expires
header。
这是一个例子:
from django.utils.cache import add_never_cache_headers
class UserPhoneNumberView(FormView):
form_class = UserPhoneNumberForm
template_name = "get_user_phonenumber.html"
def form_valid(self, form):
phonenumber = self.request.POST.get("mobile_number")
unique = self.request.POST.get("unique")
url = "http://example.com/"+unique
response = HttpResponse("", status=302)
body = "See this url: "+url
nonhttp_url = "sms:"+phonenumber+"?body="+body
response['Location'] = nonhttp_url
# This will add the proper Cache-Control and Expires
add_never_cache_headers(response)
# Now just add the 'Vary' header
response['Vary'] = '*'
return response