Django -- 弄乱测试服务器 -- 304 错误等等

发布于 2024-12-09 10:28:13 字数 603 浏览 1 评论 0原文

在开发我的网络应用程序(需要登录整个网站)时,我注意到一个有趣的“故障”(如果你可以这样称呼它)。我正在查看我的 Django 站点,并在 Chrome 中登录到该站点(在测试服务器上)。然后我退出了我的网站并完全关闭了 Chrome。

然后我打开 Firefox 并将 URL 粘贴到我的 Django 站点,期望被带到登录页面。然而,我并没有被要求登录,而是已经登录了。所以我继续注销该网站并关闭 Firefox。

然后我重新打开Chrome登录,却发现没有CSS样式。我登录后,整个网站都失去了 CSS 样式,并且测试服务器一直给出所有 CSS 和 IMG 文件的 304 错误。

我发现 304 错误与时间不匹配有关(就像 Django 认为我已注销,但在 Firefox 中我已登录 - 所以现在很困惑)。

我只是想知道这是否可以避免,或者它是否是测试服务器的“功能”?这种事情在生产模式下也会发生吗?


编辑以明确我的要求:

当我在 Django 测试服务器上遇到 304 错误时,样式和图像是否会继续发生继续发生使用 Apache 启动 Web 应用程序?

谢谢!

While working on my webapp (which requires a login for the entire site) I noticed a funny "glitch" (if you can call it that). I was viewing my Django site, logged into the site (on the test server) in Chrome. I then logged out of my site and closed Chrome entirely.

I then opened Firefox and pasted in the URL to my Django site, expecting to be taken to the login page. However, I was not asked to log in, but rather was already logged in. So I proceeded to log out of the site and close Firefox.

I then re-opened Chrome to log in, only to find that there was no CSS styling. I logged in and the whole site had lost its CSS styling and the test server kept given the 304 errors for all CSS and IMG files.

I've figured out that the 304 error has something to do with time mis-matching (like Django thinking I was logged out, but in Firefox I was logged in -- so now it is confused).

I was just wondering if this is avoidable, or if it is a "feature" of the test server? Will this kind of thing happen in production mode too?

EDIT for clarity of what I'm asking:

Will the loss of styling and images due to the 304 error that I am experiencing with the Django test server continue to occur when I launch the webapp with Apache?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆流年 2024-12-16 10:28:13

您对三件事有疑问:

  1. 为什么您在 Firefox 中登录。
  2. 为什么你在 Chrome 中丢失了 CSS 样式。
  3. 那些 304 响应是什么?

不幸的是,对于您看到的前两个问题,有很多可能的解释。很可能没有什么太严重的问题。

对于前两个问题,您可以尝试恢复到新状态,然后查看问题是否仍然出现。例如,使用干净的 Django 数据库(没有会话)并尝试清除所有缓存的浏览器信息(cookie、身份验证、缓存文件)。诚然,这是一种相当耗时的测试问题的方法,但这是我在无法直接检查您的情况的情况下可以给出的最佳一般建议。

幸运的是,304 响应根本不是问题。 304 仅表示浏览器的缓存中有该文件。请参阅条件视图处理

当客户端下次请求相同的资源时,它可能会发送一个标头,例如 If-modified-since,其中包含上次发送的修改时间的日期,或 If-none-match,包含它所在的 ETag发送。如果页面的当前版本与客户端发送的 ETag 匹配,或者资源没有被修改,则可以发回 304 状态码,而不是完整响应,告诉客户端没有任何更改。

You've got questions about three things:

  1. Why you were logged in in Firefox.
  2. Why you lost your CSS styling in Chrome.
  3. What all those 304 responses are.

Unfortunately there are quite a few possible explanations for the first two issues you're seeing. Most likely there's nothing too serious going wrong.

For the first two issues you could try reverting to a fresh state and then seeing if the problems still occur. e.g. Use a clean Django database (with no sessions) and try to clear all your cached browser information (cookies, auth, cached files). Admittedly this is quite a time consuming way to test for problems, but its the best general advice I can give without being able to examine your situation directly.

Luckily 304 responses aren't a problem at all. A 304 just means that the browser has the file in its cache. See Conditional View Processing:

When the client next requests the same resource, it might send along a header such as If-modified-since, containing the date of the last modification time it was sent, or If-none-match, containing the ETag it was sent. If the current version of the page matches the ETag sent by the client, or if the resource has not been modified, a 304 status code can be sent back, instead of a full response, telling the client that nothing has changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文