Getting 'str' object has no attribute 'get' in Django

67,071

Solution 1

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)

Solution 2

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')
Share:
67,071
user3485393
Author by

user3485393

Updated on July 09, 2022

Comments

  • user3485393
    user3485393 almost 2 years

    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.

  • user3485393
    user3485393 about 10 years
    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
    user3485393 about 10 years
    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
  • DavidM
    DavidM about 10 years
    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)).
  • Prashant Gaur
    Prashant Gaur about 10 years
    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 .
  • user3485393
    user3485393 about 10 years
    @PrashantGaur Actually logic is simple here. I am just requesting to a url which is calling a method written in views.py
  • user3485393
    user3485393 about 10 years
    What if, I have to print the value of number here. I am using pdb.settrace() but it is printing number as a string
  • Prashant Gaur
    Prashant Gaur about 10 years
    can you by converting number = int(number) only for testing purposse
  • DavidM
    DavidM about 10 years
    What is the output of print repr(number), re.search('[\d\(\)\- \+]+$', number) after the with line?
  • user3485393
    user3485393 about 10 years
    @PrashantGaur : invalid literal for int() with base 10: 'number'
  • Prashant Gaur
    Prashant Gaur about 10 years
    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
  • user3485393
    user3485393 about 10 years
    @PrashantGaur this is the url http://127.0.0.1:8000/voice/number?id=2083522792, How to check it with pdb?
  • user3485393
    user3485393 about 10 years
    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']}>,
  • DavidM
    DavidM about 10 years
    I thought that might be the case. Updated my answer.
  • user898763452
    user898763452 over 9 years
    It is return HttpResponse(resp) instead of return Httpresponse(resp).