In a Django mobile web app I'm building, I'm using the sms
html tag in one particular template. I.e. the typical <a href="sms:/* phone number here */?body=/* body text here */">Link</a>
. Every time the user presses Link
, they're redirected to their phone's default SMS app with a prepopulated message.
How can one implement a counter that increments every time a user clicks Link
? The challenge is to solely use Python/Django (server-side), no JS.
答案 0 :(得分:3)
您可以实施模型来跟踪Link
的点击次数。要跟踪,您可以创建重定向视图之类的内容,在跟踪点击后重定向到sms
URI。
一个基本的例子是:
from django.http.response import HttpResponseRedirect, HttpResponseRedirectBase
HttpResponseRedirectBase.allowed_schemes += ['sms']
class SMSRedirect(HttpResponseRedirect):
pass
def track_count(request):
phone = request.GET.get('phone', '')
body = request.GET.body('body', '')
link = build_sms_link(phone, body)
link.hits += 1
link.save()
return SMSRedirect(link.url)
默认情况下,HttpResponseRedirectBase
不允许非网络方案/协议。您可以通过猴子修补其允许的方案列表来允许它。