“Best” way to integrate Django with an Ajax library

前端 未结 5 2050
后悔当初
后悔当初 2021-02-03 13:25

Obviously, horses for courses, but what are some good ways to integrate javascript libraries with one\'s Django application?

I\'m planning on using jQuery, mostly becaus

5条回答
  •  青春惊慌失措
    2021-02-03 14:11

    I've always just made my own views which serve up JSON, and written the JavaScript myself (normally using jQuery). Obviously, this all depends on what you're trying to do - if there's a specific need you've got which an existing app solves, then by all means use it.

    Serving up JSON is pretty trivial (just dump out some JSON and return it as an HttpResponse), like this:

    def get_user_ids(request):
        if not request.is_ajax():
            raise Http404
    
        return HttpResponse(simplejson.dumps({'ids': [u.pk for User.objects.all()]}))
    

    Code above intended to be demonstrative, I'm not suggesting you make a view which shows all your user IDs.

提交回复
热议问题