Django的应用程式使用基于类的意见和Ajax?程式、意见、Django、Ajax

2023-09-11 00:46:31 作者:物极必反

我正在学习Django的,我发现基于类的意见,我不知道如何实现Ajax的这些看法。

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

我搜索github上了一个Django项目,我发现了一些使用基于类的意见,但没有阿贾克斯。

I searched github for a django project and I found some using class-based views but not ajax.

所以...任何人都知道,使用这两种东西的一个开源项目?它更容易学习的方式。

So... Anybody knows an open source project that use both things? It easier to learn that way.

感谢您:)

推荐答案

这是阿贾克斯的看法是没有太大的区别,只不过通常要处理一个正常的请求时返回不同的格式,那么一个普通视图。这种格式通常是JSON。

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.

该文件有一个可以用来返回JSON一个mixin的例子,所以这是一个很好的起点:

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

您是否希望您的视图与AJAX请求回复到正常的请求或只处理?如果是前者,唯一的诀窍是在一个小检查中render_to_response方法来拒绝任何正常的GET请求写。如果是后者,上面的链接继续讨论的情况下,你可以创建一个视图,将处理Ajax请求,并与正常的请求。

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.