Django静态CSS文件未加载:404错误求助
Hey there, let's figure out why your design.css is throwing a 404 while JS and images load just fine. First, let's zero in on the key clue from your log:
[02/May/2018 09:41:21] "GET /static/css/design.css%25 HTTP/1.1" 404 1761
That %25 is URL-encoded for a plain % character—so your server is actually receiving a request for design.css% (with a trailing percent sign). That's why it can't find the file, since your actual CSS is named design.css without that extra %.
Here are the most likely fixes to check:
1. Fix the CSS Reference in Your Templates
This is the most common culprit. Double-check how you're loading the CSS in your template files:
- Did you accidentally add an extra
%when using thestatictemplate tag? For example, did you write{% static 'css/design.css%' %}instead of the correct{% static 'css/design.css' %}? - Are you using string concatenation or variables to build the URL? Maybe a variable or template filter is appending an unwanted
%to the path (like{{ css_path }}%wherecss_pathalready includes the filename).
Quick test: View the source code of your webpage, find the link tag for design.css, and check if the href attribute ends with %. If yes, that's exactly where the problem lives.
2. Verify Static File Collection & Paths
Even though JS and images work, let's rule out static file setup issues:
- Head to your root
staticfolder (the one generated bycollectstatic) and confirmcss/design.cssexists there. If it's missing, re-runpython manage.py collectstaticto make sure all static files are copied over correctly. - Double-check your
settings.pyfor theSTATIC_ROOTconfiguration—it should point to that root static folder. For example:STATIC_ROOT = os.path.join(BASE_DIR, 'static') - Ensure the file permissions for
design.cssare correct (it should be readable by your web server process).
3. Check for Unusual URL or Static File Configurations
Since JS and images load, this is less likely, but worth a quick check:
- If you're running with
DEBUG=False, make sure your web server (like Nginx or Apache) is configured to serve static files correctly fromSTATIC_ROOT. Again, since other static assets work, this is probably not the issue, but it's good to confirm. - If you're using a custom static file storage backend, verify it's not modifying the file paths incorrectly (adding extra characters like
%).
Quick Debug Step
Try directly accessing http://your-domain/static/css/design.css (without the %25) in your browser. If the CSS loads fine, that confirms the problem is definitely in how the URL is being generated in your templates.
内容的提问来源于stack exchange,提问作者Alberto Sanmartin Martinez




