2

To better understand the language I started working on building out a client that consumes the Hacker News api. The below code is supposed to pull in the first 10 items from their jobs feed. It first gathers a list of the id's, iterates through those ID's, and makes an async request to get the item details, which fires the callback function when done. All of that is working fine. It appears however that the render function isn't returning anything when nested like this. When I hit this route, I get the following error message: The view feed.views.index didn't return an HttpResponse object. It returned None instead.

If I was coding this with Node, it would work fine so I am assuming that this is just a matter of me not understanding how the language (or possibly framework) does things with async requests and/or scoping. What am I doing wrong?

 def index(request):
    job_ids = firebase.get('jobstories', None)
    stories = []

    def append_story(story):
        stories.append(story)
        print(len(stories))
        if len(stories) == 10:
            return render(request, 'feed/index.html', {'items': stories})

    count = 0
    for ts_id in job_ids:
        if count < 10:
            count += 1
            firebase.get_async('item/' + str(ts_id), None, callback=append_story)
Zoe
  • 27,060
  • 21
  • 118
  • 148
jeffskelton3
  • 482
  • 2
  • 14
  • 1
    Django doesn't do async (i.e. all views must return a response object). Django channels looks interesting (but I haven't tried them/it). – thebjorn May 06 '16 at 20:12
  • yeah when I do synchronous calls (`firebase.get('item/' + str(ts_id))` that works fine but its slooooooow. I'll look into channels thanks. – jeffskelton3 May 06 '16 at 20:15
  • How'd do that? I need to have it even it is slow. Please give a code snippet on using the async calls as you said @jeffskelton3 – Mohith7548 Jul 10 '19 at 08:57

1 Answers1

1

First of all you probably don't want to define append_story within the index function.

Secondly, the reason you are failing is because every Django view must return a HttpResponse with something like return HttpResponse("Hello"). Since python/django doesn't support async out of the box, if you want to do async, consider using ajax instead. Here's an example of how you might do something like that: Django - How to use asynchronous task queue with celery and redis.

Basically the idea is to just generate a view then use javascript to call an ajax view that pulls your firebase info and then update your page appropriately after getting the result.

Community
  • 1
  • 1
Ringil
  • 6,277
  • 2
  • 23
  • 37
  • Yeah using JavaScript/Ajax would be my first port of call. I'm already a very experienced JS developer so I'm trying to do everything via Python so I can learn it. I've been looking at celery already and your link confirms my instinct. Appreciate the help. – jeffskelton3 May 06 '16 at 21:41