python – 从基于Django类的视图的form_valid方法调用特殊(非HTTP)URL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 从基于Django类的视图的form_valid方法调用特殊(非HTTP)URL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1918字,纯文字阅读大概需要3分钟。
内容图文
![python – 从基于Django类的视图的form_valid方法调用特殊(非HTTP)URL](/upload/InfoBanner/zyjiaocheng/773/217327d747dd4bd495a5afd774081e70.jpg)
有这个HTML技巧,如果你做< a href =“sms:14085551212?body = Hello my friend”> New SMS Message< / a> ;,点击New SMS Message打开手机的原生短信应用并预先填写要提供的数字字段(在这种情况下为1-408-555-1212),并提供消息的正文(在这种情况下我的朋友是Hello). 有没有什么办法可以从基于Django类的视图的form_valid方法中调用这个相同的href字符串?确切地说,在这个form_valid方法中,我收到的是一个uuid的POST变量.我需要在上面的例子中写的href字符串的body部分使用那个uuid. 注意:我尝试了这个答案中遵循的方法:Django 1.4 – Redirect to Non-HTTP urls它没有解决我的问题 – 我实际上没有重定向到任何地方,也没有得到错误.浏览器中的URL也不会更改.
我的代码是:
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
解决方法:
我想你应该尝试@ Selcuk的建议:从FormView返回一个模板,其HTML代码类似于:
<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>
所以你的django视图将成为:
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)
我刚刚尝试使用手机,我被重定向到我的短信应用程序.
内容总结
以上是互联网集市为您收集整理的python – 从基于Django类的视图的form_valid方法调用特殊(非HTTP)URL全部内容,希望文章能够帮你解决python – 从基于Django类的视图的form_valid方法调用特殊(非HTTP)URL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。