1

I want to serialize my Sounds model for use in my game.js file. This is what I have.

views.py

def index(request):
    context = {'sounds': serializers.serialize('json', Sounds.objects.all()) }
    return render(request, 'index.html', context) 

index.html

<button type="submit" onclick="main()">Let's Start!</button>

game.js

function main(){
    var data = {{ context|safe }};
    // print all objects here 
}

It's not working - not sure what the issue is. Basically, when I click the button in index.html, it should go to the main function (this part works), and then set a variable data with the objects in the model so that I can use it. Also, is it possible to filter objects in data so that I have a list with only the objects with id = 1? I want to do this in game.js (not in views.py or elsewhere).

1 Answer 1

1

It's hard to tell what the exact problem with the serialiser is without an actual error log. If you can actually open the console that django is running in, it should display the error when it occurs. From that a better diagnostic of the issue can be made. It would be wise to do this in the future otherwise questions like this will be downvoted.

Yes, you can filter arrays in Javascript using the filter function. The function creates a new array by interating through the objects in the array that will be passed into a function, which should return true if it should exist in the new array or false it is shouldn't.

So for the example you provided, it would appear as so:

sounds = sounds.filter((sound) => { sound.id === 1; });

Note you will have to assign the new array, the original array will not change bu running the function itself.

I highly recommend you search for solutions to these problems yourself before creating threads on Slack Overflow or your reputation will quickly decrease.

6
  • Yeah sorry. I think I've clicked on nearly every single question with "django model" and "serialization" in the title, and while there were questions with situations nearly identical to mine, the solutions didn't work for me. Also, the problem is that Django isn't throwing an "error", but there seems to be something wrong with the line in the javascript file because when I comment it out it runs okay, but the button stops working when I leave it in.
    – sy89
    Apr 17, 2018 at 0:05
  • When I mean search yourself, this can extend beyond Stack Overflow. Some quick google searches could provide a solution.. Can you add the line of code causing the main issue text (and context) so myself for other users can help diagnose the issue?
    – Alister
    Apr 17, 2018 at 0:14
  • I've done plenty of that too, and being completely new to Django (and web dev altogether), I can't seem to find a quick solution. I know beginner questions can be annoying, but I'm only posting this after trying many other solutions. The lines that I added in the question are the only ones pertinent to the situation. Like I said in my previous comment, if I comment out the var data line in game.js, it "works" (as in, the button press initiates the main function correctly). If I leave that line in, the button press doesn't work. The Django console raises no error.
    – sy89
    Apr 17, 2018 at 0:20
  • stackoverflow.com/questions/42020303/… - this is one really similar question but it doesn't work for me. I realize I should have changed var data = {{ context|safe }}; to var data = {{ sounds|safe }};, but it doesn't work regardless.
    – sy89
    Apr 17, 2018 at 0:26
  • I'm not trying to attack you, but literally posting "I have problem I don't know why is happening, please solve" is asking a lot - it's like saying "whats the answer to the this mathematical problem that involves addition" and not saying what the numbers are, and the best recommendation people can give it to learn what addition actually is. As for the line {{ context | safe }} - I don't know what is even is to be honest. It possible the code runs without it because the syntax is incorrect? If messages aren't appearing the console, check to make sure you aren't filtering warnings or errors.
    – Alister
    Apr 17, 2018 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.