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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user3485393
  • 443
  • 1
  • 4
  • 5

2 Answers2

99

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)
Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
  • 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 – user3485393 Apr 01 '14 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 . – Prashant Gaur Apr 01 '14 at 14:25
  • 1
    It is `return HttpResponse(resp)` instead of `return Httpresponse(resp)`. – user898763452 Jan 14 '15 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')
DavidM
  • 1,417
  • 1
  • 13
  • 16
  • 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 – user3485393 Apr 01 '14 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 01 '14 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 – user3485393 Apr 01 '14 at 14:25
  • What if, I have to print the value of `number` here. I am using `pdb.settrace()` but it is printing `number` as a string – user3485393 Apr 01 '14 at 14:27
  • can you by converting number = int(number) only for testing purposse – Prashant Gaur Apr 01 '14 at 14:29
  • 1
    What is the output of `print repr(number), re.search('[\d\(\)\- \+]+$', number)` after the `with` line? – DavidM Apr 01 '14 at 14:29
  • @PrashantGaur : invalid literal for int() with base 10: 'number' – user3485393 Apr 01 '14 at 14:34
  • i am sure you are sending wrong data do your url . please check value of print number ... you can easily get what wrong you are doing – Prashant Gaur Apr 01 '14 at 14:40
  • @PrashantGaur this is the url `http://127.0.0.1:8000/voice/number?id=2083522792`, How to check it with `pdb`? – user3485393 Apr 01 '14 at 14:44
  • 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 , ` – user3485393 Apr 01 '14 at 14:51
  • I thought that might be the case. Updated my answer. – DavidM Apr 01 '14 at 15:04