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

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'),

每当我请求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-     

完整回溯:

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')

我刚刚开始学习Django

参考方案

您不能直接将str作为django response传递。
您必须使用

from django.http import HttpResponse

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

返回HttpResponse(resp)

TypeError:'str'对象不支持项目分配,带有json文件的python - python

以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…

AttributeError:'AnonymousUserMixin'对象没有属性'can' - python

烧瓶学习问题为了定制对匿名用户的要求,我在模型中设置了一个类: class MyAnonymousUser(AnonymousUserMixin): def can(self, permissions): return False def is_administrator(self): return False login_manager.anonymous…

单行的'if'/'for'语句是否使用Python样式好? - python

我经常在这里看到某人的代码,看起来像是“单线”,这是一条单行语句,以传统的“if”语句或“for”循环的标准方式执行。我在Google周围搜索,无法真正找到可以执行的搜索类型?任何人都可以提出建议并最好举一些例子吗?例如,我可以一行执行此操作吗?example = "example" if "exam" in exam…

为什么使用'=='或'is'比较字符串有时会产生不同的结果? - python

我有一个Python程序,其中将两个变量设置为'public'值。在条件表达式中,我有比较var1 is var2失败,但如果将其更改为var1 == var2,它将返回True。现在,如果我打开Python解释器并进行相同的“是”比较,则此操作成功。>>> s1 = 'public' >>…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…