【问题标题】:How to set Cache-Control of a URL embedded within a special non-http URL (Python/Django)如何设置嵌入在特殊非 http URL (Python/Django) 中的 URL 的 Cache-Control
【发布时间】:2016-05-24 09:49:35
【问题描述】:

背景:

在我的 Django viewform_valid 方法中,我正在通过 字符串连接 构造一个 URL。假设上述 URL 为 http://example.com/uuid,其中 uuid 是传递给此 form_valid 方法的 POST 变量。

在这个方法中,接下来我将构建一个自定义的HttpResponse 对象,该对象将导致一个非http url,即nonhttp_url = "sms:"+phonenumber+"?body="+bodybody 包含一些文本和我之前通过字符串连接形成的 url(例如“转到此 url:[url]”)。 phonenumber 是任何合法的手机号码。

这样构造的HttpResponse 对象实际上是一个HTML 技巧,用于打开手机的本机短信应用程序,并在其中预先填写电话号码和短信正文。它的常见用法是<a href="sms:phonenumber?body="+body">Send SMS</a>。我本质上是在调用相同的东西,但是是从我的 Django 视图的 form_valid 方法内部调用的。

问题:

如何确保我通过上面的 str 连接形成并传递到非 http url 的 body 部分的 URL 将 Cache-Control 设置为 no-cache?其实我也想要no-storemust-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

【问题讨论】:

    标签: python html django url httpresponse


    【解决方案1】:

    我认为您会像处理 HTTP URL 一样执行此操作吗?您可以通过using the response as a dictionary 设置标头,就像您设置Location 标头一样。在这种情况下,添加如下行:response['Vary'] = '*'

    为方便起见,您可以使用add_never_cache_headers() 添加Cache-ControlExpires 标头。

    这是一个例子:

    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
    

    【讨论】:

    • 是的,我知道你的意思。但我在想,当我们这样做时,我们是否不会将no-cachesolely 添加到我们构建的nonhttp url 中?例如,在您编写的代码中,url 本身(定义为url = "http://example.com/"+unique)还没有设置其Cache-Control,对吗?我们只设置了它所属的非http url 的Cache-Control,不是吗?还是我错过了什么?
    • 好吧,Cache-Control 是在 HTTP 响应上设置的,而不是在 URL 上。如果您希望在来自"http://example.com/"+unique url 的响应上设置标头,则必须确保该URL 返回带有这些标头的响应。除了当前响应之外,您无法指定响应的标题。如果您还控制要发送的 URL,那应该很容易做到。
    • 我明白你的意思了。
    猜你喜欢
    • 2018-11-15
    • 2019-01-20
    • 2015-03-13
    • 1970-01-01
    • 2013-05-21
    • 2016-05-24
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    相关资源
    最近更新 更多