我有一个Django自定义网址,在POSTMAN中工作正常,但在浏览器中无法正常工作,详细信息如下所示。
在邮递员中,我使用以下网址并且工作正常 127.0.0.1:8000/v0/call_letter_status/ 而且我得到了200响应和输出
但是当我在浏览器中尝试时,我收到类似这样的错误
在/ v0 / call_letter_status /
的ValueErrorThe view project.views.User.call_letter_track didn't return an HttpResponse object.
Request Method: GET
Request URL: http://127.0.0.1:8000/v0/call_letter_status/
Django Version: 1.5
Exception Type: ValueError
我的代码如下:
def call_letter_track(request):
if request.META["CONTENT_TYPE"] == 'application/json':
if request.method == 'GET':
sqlQuery = """ SELECT jc.company_name,jc.job_position,jc.venue,jc.email_body,jc.interview_date,aj.job_id,aj.logo_image_url FROM jr_call_letter jc
JOIN api_job aj ON aj.job_id=jc.job_id ORDER BY "jc.job_id" DESC LIMIT 2 """
cursor.execute(sqlQuery)
result=dictfetchall(cursor)
final_response_map = []
key=0
for result_new in result:
print key
response_map = {}
response_map['company_name']=result[key]['company_name']
response_map['job_id']=result[key]['job_id']
response_map['job_position']=result[key]['job_position']
response_map['interview_date']=datetime.fromtimestamp(result[key]['interview_date']).strftime('%d-%m-%Y')
response_map['email_body']=result[key]['email_body']
response_map['venue']=result[key]['venue']
response_map['logo_image_url']=result[key]['logo_image_url']
key=key+1
final_response_map.append(response_map)
response = {'data':final_response_map}
data = json.dumps(response, encoding="ISO-8859-1")
return HttpResponse(data,content_type="application/json", status=200)
请帮我解决这个问题。
答案 0 :(得分:2)
您的return
声明在if
条件内。如果该条件无效,它将超出条件并期望Response
,但没有超出条件的返回,因此错误。
尝试提供此功能以进行检查:
def call_letter_track(request):
if request.META["CONTENT_TYPE"] == 'application/json':
'''
.
.
your code
.
.
'''
return HttpResponse(data,content_type="application/json", status=200)
return HttpResponse('Hello World')
默认情况下,浏览器的Content-Type
标题为application/xml
,因此未输入if
条件。
答案 1 :(得分:1)
浏览器不会向CONTENT_TYPE
发送application/json
标头;导致永远执行外部if
块;视图功能不会返回。
删除最外面的if
如何使没有Content-type: application/json
的请求也获得HttpResponse?
def call_letter_track(request):
if request.META["CONTENT_TYPE"] == 'application/json': # <---
if request.method == 'GET':
....