Django应用程序使用基于类的视图和ajax? [英] Django apps using class-based views and ajax?

查看:124
本文介绍了Django应用程序使用基于类的视图和ajax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Django,我发现基于类的视图,我不知道如何在这些视图上实现Ajax。



我搜索了github进行django项目,发现一些使用基于类的视图,但不是ajax。



所以...任何人都知道一个使用这两个东西的开源项目?



谢谢:)

解决方案

ajax视图与普通视图没有什么不同,只不过通常要在处理正常请求时返回不同的格式。这种格式通常是JSON。



该文档有一个可用于返回JSON的mixin示例,因此这是一个很好的起点:



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



您是否希望您的视图回复正常请求或仅处理AJAX请求?如果是前者,唯一的技巧就是在render_to_response方法的小写中写入拒绝任何正常的GET请求。如果是后者,上述链接继续讨论一种情况,您可以创建一个将处理ajax请求和正常请求的视图。


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-based views but not ajax.

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

Thank you :)

解决方案

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.

这篇关于Django应用程序使用基于类的视图和ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆