背景:
在我的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技巧,用于打开手机的本机SMS应用程序并预先填写电话号码和SMS正文。 它的常见用法是<a href="sms:phonenumber?body="+body">Send SMS</a> 。我实际上是在调用相同的东西,但是从我的Django视图的form_valid方法中调用。
题:
如何确保我通过上面的str连接形成的URL并传递到非http url的body部分,并将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 responseBACKGROUND:
In the form_valid method of a Django view of mine, I am constructing a URL via string concatenation. Imagine the said URL to be http://example.com/uuid, where uuid is a POST variable passed to this form_valid method.
In this method, I will next be constructing a custom HttpResponse object that will lead to a non-http url, being nonhttp_url = "sms:"+phonenumber+"?body="+body. body contains some text and the url I formed earlier via string concatenation (e.g. 'go to this url: [url]'). phonenumber is any legit mobile number.
The HttpResponse object thus constructed is actually an HTML trick that is used to open the native SMS app of a phone and pre-fill it with a phone number and SMS body. It's common usage is <a href="sms:phonenumber?body="+body">Send SMS</a>. I'm essentially calling the same thing, but from inside the form_valid method of my Django view.
QUESTION:
How do I ensure that the URL I formed via str concatenation above and passed into the body section of the non-http url has Cache-Control set to no-cache? In fact, I also want no-store and must-revalidate. Similarly, I also need Pragma set to no-cache, Expires set to 0 and Vary set to *.
CURRENT CODE:
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那样做吗? 您可以使用响应作为字典来设置标头,就像使用Location标头一样。 在这种情况下,添加如下行: response['Vary'] = '*' 。
为方便起见,您可以使用add_never_cache_headers()添加Cache-Control和Expires标头。
这是一个例子:
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 responseI'd think that you'd do it just as you would for an HTTP URL? You'd set the headers by using the response as a dictionary, just like you are for the Location header. In this case, adding lines like: response['Vary'] = '*'.
As a convenience, you could use add_never_cache_headers() to add the Cache-Control and Expires headers.
Here's an example:
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更多推荐
url,body,form_valid,HttpResponse,phonenumber,电脑培训,计算机培训,IT培训"/> <
发布评论