Flask网站在Windows正常运行但Mac端出现404错误的技术求助
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 namedweek-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.htmlvs your actual template file nameCore-Purchase.htmlvs your actual template file namePlus-Purchase.htmlvs 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

