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