如何在 Django 中集成 304?
当用户请求具有相同数据的相同页面时...我希望 Django 返回 304,这样浏览器就不必重新加载页面。
我对此很陌生。这怎么能做到呢?
谢谢。
When a user requests the same page, with the same data...I'd like Django to return a 304, so that the browser doesn't have to load the page all over again.
I'm new to this. How can this be done?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Django 文档中有详细的描述:条件视图处理
工具特别有用:
@last_modified
和@etag
视图装饰器。您为他们提供一个函数来计算请求的值,其他一切都会自动完成。There's extensive description in Django documentation: Conditional view processing
Following tools are particularly useful:
@last_modified
and@etag
view decorators. You supply them with a function to compute the value from request and everything else is done automatically.django.middleware.http.ConditionalGetMiddleware
-- it generates required ETag and returns 304 if there's a cache hit, but this still takes server time to generate full HTML and only network time is saved. Still very good for one-line configuration change.您可以查看 Django 的 缓存系统,或者您是否可以轻松检查是否用户请求相同的数据,您可以返回
HttpResponseNotModified()
- 这将返回 304。查看文档 此处。You could look into Django's caching system, or if you can easily check if the user is requesting the same data, you can return a
HttpResponseNotModified()
- this returns a 304. Check out the docs here.