简体   繁体   English

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

[英]Getting 'str' object has no attribute 'get' in Django

views.py视图.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网址.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

You can not pass directly str as a django response .您不能直接将str作为django response传递。 You must use你必须使用

from django.http import HttpResponse

if you want to render string data as django view response.如果要将字符串数据呈现为 django 视图响应。 have a look django.http.HttpResponse看看django.http.HttpResponse

return HttpResponse(resp)

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.此外, generate_xmlnumber变量将仅包含字符串'number' ,而不包含 GET 参数。 To get that, you might use:为此,您可以使用:

request.GET.get('id')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;str&#39;对象在django中没有属性&#39;get&#39; - AttributeError: 'str' object has no attribute 'get' in django Django: 'str' object 没有属性 'get' - Django: 'str' object has no attribute 'get' 收到此错误“str”object 没有属性“get” - getting this error 'str' object has no attribute 'get' 'str' object has no attribute 'get' attribute error in django - 'str' object has no attribute 'get' attribute error in django 尝试使用 Django 设置 SendGrid 的电子邮件 API 时出现此错误 - AttributeError: &#39;str&#39; object has no attribute &#39;get&#39; - Getting this error while trying to set SendGrid's email API with Django - AttributeError: 'str' object has no attribute 'get' AttributeError at /add/…Getting 'str' object has no attribute 'get' in Django.Atrri - AttributeError at /add/…Getting 'str' object has no attribute 'get' in Django.Atrri 请求Django脚本时,“ str”对象没有属性“ GET” - 'str' object has no attribute 'GET' when requesting to django script AttributeError: 'str' object 没有属性 'get'(django 视图) - AttributeError: 'str' object has no attribute 'get' (django view) Django 查看 AttributeError: 'str' object 没有属性 'get' - Django view AttributeError: 'str' object has no attribute 'get' 将 slug 传递给 django 表单时,“'str' object 没有属性 'get'” - "'str' object has no attribute 'get'" when passing slug to django form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM