views.py文件:
def get_pattern(request, product_id):
"""
Get JSON for needed pattern
"""
data = Patterns.objects.get(related_module=product_id)
product_data = serializers.serialize("json", [data, ])
return product_data
urls.py文件:
urlpatterns = [
url(r'^get_pattern(?P<product_id>[0-9]+)/$', views.get_pattern, name='get_pattern'),
]
错误信息:
Request Method: GET
Request URL: http://xxxxxxx:8000/xxxx/get_pattern1/
Django Version: 1.8.3
Exception Type: AttributeError
Exception Value:
'unicode' object has no attribute 'get'
Exception Location: /home/xxxx/local/lib/python2.7/site- packages/django/middleware/clickjacking.py in process_response, line 31
错误的地方:
return product_data
django的view返回的必须是HttpResponse对象,而不是String。在后台会默认返回HttpResponse对象而调用get()方法,由于字符串没有get()方法导致报错。
修改:
bytes = product_data.encode('utf-8')
return django.http.HttpResponse(bytes, content_type='application/json')
博客指出在Django中,views.py文件出现错误,原因是视图返回的是字符串而非HttpResponse对象。后台默认返回HttpResponse对象并调用get()方法,而字符串无此方法,从而导致报错,还提及了修改方向。
4608

被折叠的 条评论
为什么被折叠?



