You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask网站在Windows正常运行但Mac端出现404错误的技术求助

Fix Flask 404 Errors on macOS (Works on Windows)

This is almost certainly a case sensitivity issue—macOS treats file names as case-sensitive by default, while Windows ignores case differences. Here's how to diagnose and fix it:

Key Problem Areas to Check

1. Template File Name Mismatches

Compare the template names in your routes.py render_template() calls to the actual files in your templates folder on macOS. For example:

  • Your code uses render_template("Week-Purchase.html"), but if your actual file is named week-purchase.html (lowercase "W"), macOS will treat this as a completely different file, resulting in a 404.
  • Double-check all these pairs in your code:
    • Day-purchase.html vs your actual template file name
    • Core-Purchase.html vs your actual template file name
    • Plus-Purchase.html vs your actual template file name

Windows lets you get away with these mismatches, but macOS won’t.

2. Git Case Handling Quirks

Windows Git defaults to ignoring case, which means you might have committed files with one case (e.g., week-purchase.html) but your code references another (e.g., Week-Purchase.html). When you pull to macOS, Git retains the original case from the commit, leading to mismatches.

Fix this for your current repo on macOS:

# Enable case sensitivity for this Git repo
git config core.ignorecase false
# Re-pull to ensure you get the correct file cases
git pull

3. Working Directory When Running Flask

Make sure you’re running app.py from the root of your project folder (the same directory that contains the templates folder). If you run it from a subdirectory, Flask won’t locate your templates, causing 404s.

Example:

# Navigate to your project root first
cd /path/to/your/flask-project
# Then run the app
python app.py

How to Verify

Run your Flask app in debug mode on macOS and check the console output. If there’s a template mismatch, you’ll see an error like:

jinja2.exceptions.TemplateNotFound: Week-Purchase.html

This directly tells you which file Flask can’t find—either rename the template to match your code, or update the render_template() call to match the file name.

Prevent Future Issues

  • Standardize on a case convention for all template files (e.g., all lowercase with hyphens: week-purchase.html) and stick to it in your code.
  • Enable Git’s case sensitivity on both Windows and macOS to avoid mismatched commits down the line.

内容的提问来源于stack exchange,提问作者PassionateFruit678

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 09:12:31