我正在寻找一个从表单接收到的号码,该号码已转换为取决于该号码的特定名称。 事先道歉,如果这是直截了当的,我的大脑就不会接受。
该表单正在通过js脚本发布到此视图“ def create account”
从这里我想将数字转换为单词,即1 =白羊座,所以当它发布到数据库时,它会显示修改后的值。
这里是代码-该值正在'ss'(star_sign)中发送
谢谢
def create_account(request):
if request.method == "POST":
if Account.objects.filter(email=request.POST['email']).exists():
return HttpResponse("dupe")
else:
ip = get_client_ip(request)
cid = ""
if "cid" in request.GET:
cid = request.GET["cid"]
account = Account.objects.create(email=request.POST['email'], first_name=request.POST['first_name'], birth_day=request.POST['birth_day'], birth_month=request.POST['birth_month'], birth_year=request.POST['birth_year'], star_sign=request.POST['ss'], ip=ip, cid=cid)
return HttpResponse("window.location.href='/lp/?id=check-email-ss&cid=" + cid + "'")
答案 0 :(得分:1)
您可以使用字典轻松实现此效果和类似效果,例如:
converter = {
1: "one",
2: "two",
3: "three",
}
converter[1]
# outputs "one"
converter[function_that_returns_a_number()]
# outputs the string corresponding to the number, if present
# gives KeyError if not found
converter.get(function_that_returns_a_number(), "Not found")
# as above, but defaults to "Not found" for values not present in the mapping