You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Django静态CSS文件未加载:404错误求助

Troubleshooting the 404 Error for Your CSS File

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 the static template 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 }}% where css_path already 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 static folder (the one generated by collectstatic) and confirm css/design.css exists there. If it's missing, re-run python manage.py collectstatic to make sure all static files are copied over correctly.
  • Double-check your settings.py for the STATIC_ROOT configuration—it should point to that root static folder. For example:
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
  • Ensure the file permissions for design.css are 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 from STATIC_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

火山引擎 最新活动