I wanted to pass an argument (form field value) in an URL as below. But when I do this, it raises an error
not enough arguments for format string
I would appreciate help me in solve this or suggest me an alternate approach for passing a form_cleaned value to HttpResponseRedirect.
def phone(request):
form = PhoneForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
Phone = form.cleaned_data.get('Phone')
instance.save()
form.save()
return HttpResponseRedirect('http://send_sms.com/api_key=api&to=%s&message=Welcome%20to%messaging' %Phone)
context = {
"form": form,
}
return render(request, "phone.html", context)
The problem is that Python is treating the other
%
signs in your string as placeholders as well.You could double up the other percent signs (e.g.
Welcome%%20
), or use.format(Phone)
, but a safer approach would be to get Python to take care of encoding the querystring for you.This is hopefully more readable and decreases the chances of mistakes. For example, in your question, you have
%
instead of%20
in%messaging
.