开发者

Django: how to redirect to mailto

开发者 https://www.devze.com 2023-01-20 19:51 出处:网络
I have a Django admin action called \"Email selected members\". Check some members and click the Go button and the user\'s mail program is opened. The emails of the sele开发者_Python百科cted members h

I have a Django admin action called "Email selected members". Check some members and click the Go button and the user's mail program is opened. The emails of the sele开发者_Python百科cted members have been pre-entered.

This works by a Django HttpResponseRedirect(uri) with the uri being "mailto:email1,email2.. where the addresses email1, email2 ... were looked up on the server.

The only problem is that that the browser re-directs to a blank page as well a opening the client mail program.

Is there any way to avoid this?

-- Peter


This question is a little old, but I just went through this and I think I can help anyone looking for an answer in the future.

I ran into this problem because the website I was building had a built-in tracking system that tracked the URLs of outbound links for self-hosted ads. If I don't redirect, there is no way (without changing the way it was implemented) to track the click, since I'm not using an API or anything.

The easy fix was to do what you did, sending back an HttpResponse() whose content is the meta tag

<meta http-equiv="refresh" content="0;url=mailto:youremail@test.com" />

This causes the page to refresh on load, which triggers the mailto: action.

Now we're left with an open window, but we can't close the window using Javascript's window.close() method. I do believe that this solution should work, however. Call that Javascript function after the refresh has been successful.

Unfortunately, I haven't tested this, but these two methods should accomplish a mailto: redirect that does not leave a blank window/tab behind.

Hope this helps!


Don't use HttpResponseRedirect. Just make the mailto: line a link. <a href="mailto:email1...">Email selected members</a>


I don't think it is possible. RFC 2616 says re 302 redirect:

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s)

So the blank page that I see is the (very) short hypertext note. The browser gets the redirect instruction, pops up a temporary page with the redirect message, then retrieves the redirected URL. But with a mailto: URL the temporary page obviously remains.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号