Reputation: 1
I am looking to a received number from a form converted to a specific name dependent on the number. Apologies in advance if this is straight forward, my brain is not with it.
The form is being posted via a js script to this view 'def create account'
from here i want to translate the number to a word i.e 1 = Aries, so when it is posted to the database it shows the revised value.
Here is the code - the value is being sent in on 'ss' (star_sign)
Thank you
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 + "'")
Upvotes: 0
Views: 114
Reputation: 2665
You can easily achieve this and similar effects with dictionaries, for example:
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
Upvotes: 1