获取'str'对象在Django中没有属性'get'

时间:2022-06-28 08:35:50

views.py

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

每当我请求http://127.0.0.1:8000/voice/number?id=98获取以下错误时:

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.

我刚刚开始学习Django。

2 个解决方案

#1


44  

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

你不能直接传递str作为django响应。你必须使用

from django.http import HttpResponse

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

如果要将字符串数据呈现为django视图响应。看看这里

return HttpResponse(resp)

#2


1  

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

Django视图必须始终返回一个HttpResponse对象,因此请尝试将该字符串包装在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:

另外,generate_xml中的number变量只包含字符串'number',而不包含GET参数。为此,您可以使用:

request.GET.get('id')

#1


44  

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

你不能直接传递str作为django响应。你必须使用

from django.http import HttpResponse

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

如果要将字符串数据呈现为django视图响应。看看这里

return HttpResponse(resp)

#2


1  

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

Django视图必须始终返回一个HttpResponse对象,因此请尝试将该字符串包装在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:

另外,generate_xml中的number变量只包含字符串'number',而不包含GET参数。为此,您可以使用:

request.GET.get('id')