4

There's this HTML trick where if you do <a href="sms:14085551212?body=Hello my friend">New SMS Message</a>, clicking New SMS Message opens up the phone's native SMS app and pre-fills the To field with the number provided (1-408-555-1212 in this case), and the body with the message provided (Hello my friend in this case).

Is there any way I can call this same href string from the form_valid method of a Django class-based view? To be exact, in this form_valid method I'm receiving a POST variable that is a uuid. I need to use that uuid in the body section of the href string I've written in the example above.

Note: I tried the approach followed in this answer: Django 1.4 - Redirect to Non-HTTP urls It doesn't solve my problem - I actually don't redirect anywhere, nor do I get an error. The URL in the browser doesn't change either.

My code is:

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'] = str(nonhttp_url)
        return response
Community
  • 1
  • 1
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
  • I had assumed you could use `HttpResponseRedirect('sms:14085551212?body=Hello my friend')` but this doesn't allow unsafe requests... not sure if this helps solve the problem at all – Sayse Feb 12 '16 at 22:47
  • This may be a [duplicate/solution](http://stackoverflow.com/q/14558876/1324033), I haven't had chance to test it though – Sayse Feb 12 '16 at 22:49
  • 1
    Giving that a shot Sayse. If it works, I'll shut this one down since it's duplicated. Otherwise I'll add my finding to the question and keep it open. Thanks for the tip :-) – Hassan Baig Feb 12 '16 at 22:51
  • The 302 status is normally "nothings changed" which seemed weird to me to use. Does the new request show in the browsers network tab? (in the dev options normally shown with f12) – Sayse Feb 13 '16 at 08:55
  • Hmm nothing happens in the browser's network tag (the original URL keeps sitting there). If I try to instead do `response['Location'] = 'http://google.com'` this whole set up executes perfectly (with multiple respective redirects showing up on the browser's network tab). – Hassan Baig Feb 13 '16 at 14:25
  • Caching perhaps? Like I said, 302 means nothings changed so maybe use a different status I.e 200 – Sayse Feb 13 '16 at 15:04
  • You can also try creating a template with ` – Selcuk Feb 13 '16 at 16:10
  • @HassanBaig - How were you testing this? I just tried it with `mailto` and it worked, and then I saw that [ios uses different syntax for sms](http://stackoverflow.com/a/19126326/1324033) – Sayse Feb 13 '16 at 18:13
  • @Sayse: Yes I know, `mailto` indeed works (but not `sms:` or `sms://` so far). The ios demographic isn't on my radar - the SMS syntax I posted is tried and tested with my users. But even if one uses the ios sytax, the redirect doesn't kick in. Nor no matter which status I try. This is an example of what ought to happen if one tries these tags in a desktop browser: http://bradorego.com/test/sms.html Maybe I need to put in an intermediate URL, that in turn redirects to the sms non-http url. Thinking aloud here. – Hassan Baig Feb 13 '16 at 18:29

1 Answers1

1

I think you should try @Selcuk's suggestion: returning a template from the FormView with HTML code similar to:

<html>
  <head>
    <meta http-equiv="refresh" content="0;URL={{ url }}" />
  </head>
  <body>
    <!-- The user will see this when they come back to the browser, 
         and if the SMS app does not open right away -->
    Waiting message 
  </body>
</html>

So your django view would become:

from urllib import quote
from django.shortcuts import render

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
        body = quote("See this url: "+url)
        nonhttp_url = "sms:"+phonenumber+"?body="+body
        context = {'url': nonhttp_url}
        return render(request, 'theTemplate.html', context)

I just tried on my phone, and I got redirected to my SMS app.

Djizeus
  • 4,161
  • 1
  • 24
  • 42
  • But isn't this an *immediate* redirect? I'm looking for a redirect that happens only when the form is submitted (because I get to process some relevant code when it happens). That's why I've been targeting `form_valid`. There's a complementary question I've posted here: http://stackoverflow.com/questions/35381763/submit-a-form-and-execute-an-a-href-tag-simultaneously – Hassan Baig Feb 13 '16 at 18:53
  • 1
    I meant returning this template from your form processing view. – Djizeus Feb 13 '16 at 18:55
  • I see what you mean. I'll get back to you in a bit. – Hassan Baig Feb 13 '16 at 18:56