Rendering JSON objects using a Django template after an Ajax call

前端 未结 8 1806
南笙
南笙 2020-12-02 03:59

I\'ve been trying to understand what\'s the optimal way to do Ajax in Django. By reading stuff here and there I gathered that the common process is:

  1. formul

8条回答
  •  悲&欢浪女
    2020-12-02 04:25

    When you are doing Ajax I don't think you have any use for templates. Template is there so that you can generate dynamic HTML on the server side easily and hence it provides few programming hooks inside HTML.

    In case of Ajax you are passing JSON data and you can format it as you want in Python. and HTML/document elements will be generated on client side using the JSON by some JavaScript library e.g. jQuery on client side.

    Maybe if you have a very specific case of replacing some inner HTML from server side HTML then maybe you can use templates but in that case why you would need JSON? You can just query the HTML page via Ajax and change inner or outer or whatever HTML.

提交回复
热议问题