Search code examples
pythondjangodjango-formsdjango-viewscleaned-data

Django/python: How to pass the form.cleaned_data value to HttpResponseRedirect as an argument?


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)

Solution

  • 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.

    from urllib.parse import urlencode # Python 3
    # from urllib import urlencode # Python 2
    
    query = {
       'api_key': 'api',
       'to': Phone,
       'message': 'Welcome to messaging',
    }
    url = 'http://send_sms.com/?%s' % urlencode(query)
    return HttpResponseRedirect(url)
    

    This is hopefully more readable and decreases the chances of mistakes. For example, in your question, you have % instead of %20 in %messaging.