The view core.views.postcodes didn't return an HttpResponse object. It returned None instead












0















I want to render a local json file I'm loading from a view in Django.



I have this function on my views.py file:



def postcodes(request):
data = open('core/stores.json').read()
jsonData = json.loads(data)


On my urls.py:



urlpatterns = [
path('stores/', views.postcodes, name='postcodes'),
]


It throws me this error:



Internal Server Error: /stores/
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/base.py", line 139, in _get_response
"returned None instead." % (callback.__module__, view_name)
ValueError: The view core.views.postcodes didn't return an HttpResponse object. It returned None instead.


I think this comes from the fact that I'm using request as a parameter on my postcodes function.



Any ideas on how can I load this json file on my view?



I'm using Django 1.11










share|improve this question


















  • 1





    Your postcodes view does not return something, it should return a HttpResponse object.

    – Willem Van Onsem
    Nov 25 '18 at 21:09






  • 1





    Note that all of this is pointless. There is no reason to parse the JSON to Python if all you want to do is to send it to the browser. This is a static file and should be handled by the static file serving system, just like your JS and CSS.

    – Daniel Roseman
    Nov 25 '18 at 21:16


















0















I want to render a local json file I'm loading from a view in Django.



I have this function on my views.py file:



def postcodes(request):
data = open('core/stores.json').read()
jsonData = json.loads(data)


On my urls.py:



urlpatterns = [
path('stores/', views.postcodes, name='postcodes'),
]


It throws me this error:



Internal Server Error: /stores/
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/base.py", line 139, in _get_response
"returned None instead." % (callback.__module__, view_name)
ValueError: The view core.views.postcodes didn't return an HttpResponse object. It returned None instead.


I think this comes from the fact that I'm using request as a parameter on my postcodes function.



Any ideas on how can I load this json file on my view?



I'm using Django 1.11










share|improve this question


















  • 1





    Your postcodes view does not return something, it should return a HttpResponse object.

    – Willem Van Onsem
    Nov 25 '18 at 21:09






  • 1





    Note that all of this is pointless. There is no reason to parse the JSON to Python if all you want to do is to send it to the browser. This is a static file and should be handled by the static file serving system, just like your JS and CSS.

    – Daniel Roseman
    Nov 25 '18 at 21:16
















0












0








0








I want to render a local json file I'm loading from a view in Django.



I have this function on my views.py file:



def postcodes(request):
data = open('core/stores.json').read()
jsonData = json.loads(data)


On my urls.py:



urlpatterns = [
path('stores/', views.postcodes, name='postcodes'),
]


It throws me this error:



Internal Server Error: /stores/
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/base.py", line 139, in _get_response
"returned None instead." % (callback.__module__, view_name)
ValueError: The view core.views.postcodes didn't return an HttpResponse object. It returned None instead.


I think this comes from the fact that I'm using request as a parameter on my postcodes function.



Any ideas on how can I load this json file on my view?



I'm using Django 1.11










share|improve this question














I want to render a local json file I'm loading from a view in Django.



I have this function on my views.py file:



def postcodes(request):
data = open('core/stores.json').read()
jsonData = json.loads(data)


On my urls.py:



urlpatterns = [
path('stores/', views.postcodes, name='postcodes'),
]


It throws me this error:



Internal Server Error: /stores/
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/base.py", line 139, in _get_response
"returned None instead." % (callback.__module__, view_name)
ValueError: The view core.views.postcodes didn't return an HttpResponse object. It returned None instead.


I think this comes from the fact that I'm using request as a parameter on my postcodes function.



Any ideas on how can I load this json file on my view?



I'm using Django 1.11







python django






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 21:08









NeoVeNeoVe

2,01662974




2,01662974








  • 1





    Your postcodes view does not return something, it should return a HttpResponse object.

    – Willem Van Onsem
    Nov 25 '18 at 21:09






  • 1





    Note that all of this is pointless. There is no reason to parse the JSON to Python if all you want to do is to send it to the browser. This is a static file and should be handled by the static file serving system, just like your JS and CSS.

    – Daniel Roseman
    Nov 25 '18 at 21:16
















  • 1





    Your postcodes view does not return something, it should return a HttpResponse object.

    – Willem Van Onsem
    Nov 25 '18 at 21:09






  • 1





    Note that all of this is pointless. There is no reason to parse the JSON to Python if all you want to do is to send it to the browser. This is a static file and should be handled by the static file serving system, just like your JS and CSS.

    – Daniel Roseman
    Nov 25 '18 at 21:16










1




1





Your postcodes view does not return something, it should return a HttpResponse object.

– Willem Van Onsem
Nov 25 '18 at 21:09





Your postcodes view does not return something, it should return a HttpResponse object.

– Willem Van Onsem
Nov 25 '18 at 21:09




1




1





Note that all of this is pointless. There is no reason to parse the JSON to Python if all you want to do is to send it to the browser. This is a static file and should be handled by the static file serving system, just like your JS and CSS.

– Daniel Roseman
Nov 25 '18 at 21:16







Note that all of this is pointless. There is no reason to parse the JSON to Python if all you want to do is to send it to the browser. This is a static file and should be handled by the static file serving system, just like your JS and CSS.

– Daniel Roseman
Nov 25 '18 at 21:16














1 Answer
1






active

oldest

votes


















1














Well here you do not return anything, the function is called, but it does not, as expected, returns a HttpResonse.



Based on your view, you probably want to return a JsonResponse [Django-doc], so we can return this with:



from django.http import JsonResponse
import json

def postcodes(request):
with open('core/stores.json') as f:
data = json.load(f)
return JsonResponse(data)


If you however do not plan to do any filtering, aggregation, or other processing, you however better move the stores.json to a static file, like @DanielRoseman says.






share|improve this answer





















  • 1





    Hi Willem, thank you very much, actually I'll filter this through template view later on, so this is really helpful for me to understand all the process, Thank You very much!

    – NeoVe
    Nov 25 '18 at 21:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472005%2fthe-view-core-views-postcodes-didnt-return-an-httpresponse-object-it-returned%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Well here you do not return anything, the function is called, but it does not, as expected, returns a HttpResonse.



Based on your view, you probably want to return a JsonResponse [Django-doc], so we can return this with:



from django.http import JsonResponse
import json

def postcodes(request):
with open('core/stores.json') as f:
data = json.load(f)
return JsonResponse(data)


If you however do not plan to do any filtering, aggregation, or other processing, you however better move the stores.json to a static file, like @DanielRoseman says.






share|improve this answer





















  • 1





    Hi Willem, thank you very much, actually I'll filter this through template view later on, so this is really helpful for me to understand all the process, Thank You very much!

    – NeoVe
    Nov 25 '18 at 21:24
















1














Well here you do not return anything, the function is called, but it does not, as expected, returns a HttpResonse.



Based on your view, you probably want to return a JsonResponse [Django-doc], so we can return this with:



from django.http import JsonResponse
import json

def postcodes(request):
with open('core/stores.json') as f:
data = json.load(f)
return JsonResponse(data)


If you however do not plan to do any filtering, aggregation, or other processing, you however better move the stores.json to a static file, like @DanielRoseman says.






share|improve this answer





















  • 1





    Hi Willem, thank you very much, actually I'll filter this through template view later on, so this is really helpful for me to understand all the process, Thank You very much!

    – NeoVe
    Nov 25 '18 at 21:24














1












1








1







Well here you do not return anything, the function is called, but it does not, as expected, returns a HttpResonse.



Based on your view, you probably want to return a JsonResponse [Django-doc], so we can return this with:



from django.http import JsonResponse
import json

def postcodes(request):
with open('core/stores.json') as f:
data = json.load(f)
return JsonResponse(data)


If you however do not plan to do any filtering, aggregation, or other processing, you however better move the stores.json to a static file, like @DanielRoseman says.






share|improve this answer















Well here you do not return anything, the function is called, but it does not, as expected, returns a HttpResonse.



Based on your view, you probably want to return a JsonResponse [Django-doc], so we can return this with:



from django.http import JsonResponse
import json

def postcodes(request):
with open('core/stores.json') as f:
data = json.load(f)
return JsonResponse(data)


If you however do not plan to do any filtering, aggregation, or other processing, you however better move the stores.json to a static file, like @DanielRoseman says.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 21:22

























answered Nov 25 '18 at 21:11









Willem Van OnsemWillem Van Onsem

150k16145235




150k16145235








  • 1





    Hi Willem, thank you very much, actually I'll filter this through template view later on, so this is really helpful for me to understand all the process, Thank You very much!

    – NeoVe
    Nov 25 '18 at 21:24














  • 1





    Hi Willem, thank you very much, actually I'll filter this through template view later on, so this is really helpful for me to understand all the process, Thank You very much!

    – NeoVe
    Nov 25 '18 at 21:24








1




1





Hi Willem, thank you very much, actually I'll filter this through template view later on, so this is really helpful for me to understand all the process, Thank You very much!

– NeoVe
Nov 25 '18 at 21:24





Hi Willem, thank you very much, actually I'll filter this through template view later on, so this is really helpful for me to understand all the process, Thank You very much!

– NeoVe
Nov 25 '18 at 21:24




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472005%2fthe-view-core-views-postcodes-didnt-return-an-httpresponse-object-it-returned%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python