简体   繁体   English

Django:无需重定向即可更新数据库的按钮

[英]Django: Button that updates database without redirection

i've found some examples, but i was wondering what's the best way to do this: 我找到了一些示例,但是我想知道这样做的最佳方法是什么:

-Having a button on the template, that (when clicked) updates something in the database, without leaving or refreshing the current page. -在模板上具有一个按钮,该按钮(单击时)将更新数据库中的某些内容,而无需离开或刷新当前页面。

I'm wondering some AJAX, jQuery will be needed. 我想知道是否需要AJAX,jQuery。 Thanks in advance. 提前致谢。 (I'm using Django). (我正在使用Django)。

Yes, as long as your view returns a 204 No Content response : 是的,只要您的视图返回204 No Content响应

return HttpResponse(status=204)

A 204 response tells your browser to not change the current page, because no new content has been returned. 204响应告诉您的浏览器不要更改当前页面,因为没有返回新内容。

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent . 如果客户端是用户代理,则不应更改导致发送请求的文档视图 This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view. 尽管任何新的或更新的元信息都应该应用于当前在用户代理的活动视图中的文档,但该响应主要旨在允许输入操作而不会导致更改用户代理的活动文档视图。

Emphasis mine. 强调我的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM