Calling a special (non-HTTP) URL from the form_valid method of a Django class-based view

☆樱花仙子☆ 提交于 2019-12-05 18:19:15

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!