Django apps using class-based views and ajax?

后端 未结 3 1339
逝去的感伤
逝去的感伤 2021-01-31 04:50

I\'m learning Django and I found class-based views and I wonder how to implement Ajax on those views.

I searched github for a django project and I found some using class

3条回答
  •  情歌与酒
    2021-01-31 05:25

    An ajax view isn't much different to a normal view except that you usually want to return a different format then when processing a normal request. This format is usually JSON.

    The documentation has an example of a mixin that can be used to return JSON, so this is a good starting point:

    https://docs.djangoproject.com/en/dev/topics/class-based-views/mixins/#more-than-just-html

    Do you want your view to reply to normal requests or only deal with AJAX requests? If the former, the only trick would be to write in a small check in the render_to_response method to reject any normal GET requests. If the latter, the above link goes on to discuss a situation where you can create a view that will deal with ajax requests and with normal requests.

提交回复
热议问题