44

views.py

def generate_xml(request, number):
    caller_id = 'x-x-x-x'
    resp = twilio.twiml.Response()

    with resp.dial(callerId=caller_id) as r:
         if number and re.search('[\d\(\)\- \+]+$', number):
            r.number(number)
         else:
             r.client('test')
   return str(resp)

url.py

url(r'^voice/(?P<number>\w+)$', 'django_calling.views.generate_xml', name='generating TwiML'),

Whenever I am requesting http://127.0.0.1:8000/voice/number?id=98 getting following error:

Request Method:     GET
Request URL:    http://127.0.0.1:8000/voice/number?id=90
Django Version:     1.6.2
Exception Type:     AttributeError
Exception Value:    'str' object has no attribute 'get'

Exception Location:     /usr/local/lib/python2.7/dist-     

Full Traceback:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/voice/number?id=90

Django Version: 1.6.2
Python Version: 2.7.5
Installed Applications:
 ('django.contrib.admin',
'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_calling',
'django_twilio',
'twilio')
 Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')

I have just started to learn Django.

2
  • Are you sure, this view is getting called? Put a pdb inside this view to make sure the view gets called. Apr 1, 2014 at 14:02
  • Post full traceback, please.
    – Maciej Gol
    Apr 1, 2014 at 14:02

2 Answers 2

100

You can not pass directly str as a django response . You must use

from django.http import HttpResponse

if you want to render string data as django view response. have a look django.http.HttpResponse

return HttpResponse(resp)
3
  • It works but it is printing r.clinet('test') not r.number(number)? Any idea? If i directly return any integer it is giving the same error Apr 1, 2014 at 14:17
  • there may be a change that it is always going into else statement . i will suggest you to cross check your re and your logic again to get mobile or client number . Apr 1, 2014 at 14:25
  • 1
    It is return HttpResponse(resp) instead of return Httpresponse(resp). Jan 14, 2015 at 23:05
6

Django views must always return an HttpResponse object, so try wrapping that string in an HttpResponse:

from django.http import HttpResponse
return HttpResponse(str(resp))

Additionally, the number variable in generate_xml will contain only the string 'number', not the GET parameter. To get that, you might use:

request.GET.get('id')
11
  • It works but it is printing r.clinet('test') not r.number(number)? Any idea? If i directly return any integer it is giving the same error Apr 1, 2014 at 14:14
  • 1
    Are you certain your regular expression is correct? Remember, anything you return must be wrapped in an HttpResponse, so if you want to return an integer, it must be return HttpResponse(str(the_number)).
    – DavidM
    Apr 1, 2014 at 14:18
  • @PrashantGaur Actually logic is simple here. I am just requesting to a url which is calling a method written in views.py Apr 1, 2014 at 14:25
  • 1
    What is the output of print repr(number), re.search('[\d\(\)\- \+]+$', number) after the with line?
    – DavidM
    Apr 1, 2014 at 14:29
  • 1
    Aha! I am so sorry . You are correct I checked with pdb and it is just giving a string number, This is the stacktrace: Pdb) request <WSGIRequest path:/voice/number, GET:<QueryDict: {u'id': [u'2083522792']}>, Apr 1, 2014 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.