2

I have a Django custom URL which works fine in POSTMAN but not working properly in a browser the details are given as below.

In postman, I am using the following URL and it s working fine 127.0.0.1:8000/v0/call_letter_status/ and I am getting a 200 response and output as well

But when I am trying in browser I am getting an error like this

ValueError at /v0/call_letter_status/

The 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

My Code is as given below:

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)

Please help me in getting a solution for this problem.

2 Answers 2

2

Your return statement is inside the if condition. If that condition is invalid it will go outside of the condition and expect a Response, but there is no return outside your condition, hence the error.

try providing this for checking:

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

The browser by default has the Content-Type header of application/xml and hence it is not entering your if condition.

Sign up to request clarification or add additional context in comments.

Comments

1

Browser will not send CONTENT_TYPE header with application/json; causes the outer if block is never executed; The view function will not return.

How about Remove the outermost if so that request without Content-type: application/json also get HttpResponse?

def call_letter_track(request):
     if request.META["CONTENT_TYPE"] == 'application/json':  # <---
         if request.method == 'GET':
             ....

2 Comments

#falsetru This one actually works but the problem with this is that,If I want to pass an ID and want to get the result for that ID as o/p then I should need to use this right???
@thomaschacko, Do you mean passing as a query string like ....?id=something ? Then, you can access the id using request.REQUEST.get('id') or request.GET.get('id')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.