django 1.5 static files 404 error

本文介绍了如何在Django项目中正确配置静态文件,包括设置STATIC_URL、使用STATICFILES_DIRS和STATICFILES_STORAGE等,确保静态资源能被正确加载。

网上找了一下,官方的解决方法


Configuring static files

  1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

  2. In your settings file, define STATIC_URL, for example:

    STATIC_URL = '/static/'
    
  3. In your templates, either hardcode the url like /static/my_app/myexample.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).

    {% load staticfiles %}
    <img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
    
  4. Store your static files in a folder called static in your app. For example my_app/static/my_app/myimage.jpg.

Now, if you use ./manage.py runserver, all static files should be served automatically at the STATIC_URL and be shown correctly.

Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files. For example:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/var/www/static/',
)

See the documentation for the STATICFILES_FINDERS setting for details on how staticfiles finds your files.



代码部分:

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

BASE_DIR = os.getcwd()

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.

    os.path.join(BASE_DIR, "static"),
)


### Nginx `error_page 404` 的配置方法 在 Nginx 配置文件中,`error_page 404` 指令用于指定当客户端请求的资源未找到(HTTP 状态码 404)时,Nginx 应该如何响应。可以在 `server` 块或 `location` 块中配置该指令。 在 `server` 块中配置: ```nginx server { listen 80; server_name your_domain_or_ip; # 当出现 404 错误时,重定向到指定的页面 error_page 404 /404.html; location / { # 其他配置 } location = /404.html { root /path/to/your/static/files; } } ``` 在 `location` 块中配置: ```nginx server { listen 80; server_name your_domain_or_ip; location /specific_path { # 当在 /specific_path 下出现 404 错误时,重定向到指定的页面 error_page 404 /specific_404.html; # 其他配置 } location = /specific_404.html { root /path/to/your/static/files; } } ``` ### `error_page 404` 的作用 `error_page 404` 的主要作用是为用户提供一个友好的错误页面,当用户请求的资源不存在时,显示自定义的错误信息,而不是默认的 Nginx 404 页面。这样可以提升用户体验,同时也可以在错误页面中提供一些有用的导航或提示信息,帮助用户继续浏览网站。 ### 结合当前情况解决 404 问题 在 Django 服务 uWSGI 返回 200 响应码但 Nginx 返回 404 响应码的情况下,`error_page 404` 本身并不能直接解决 404 问题,但可以帮助诊断和调试。 #### 利用 `error_page 404` 进行调试 可以配置一个简单的调试页面,当出现 404 错误时,显示一些额外的信息,例如请求的 URL、请求的时间等。 ```nginx server { listen 80; server_name your_domain_or_ip; error_page 404 /debug_404.html; location = /debug_404.html { root /path/to/your/debug/files; add_header Content-Type text/html; return 200 "<html><body><h1>404 Error</h1><p>Requested URL: $request_uri</p><p>Time: $time_local</p></body></html>"; } location / { # 其他配置 } } ``` #### 解决 404 问题的其他建议 - **检查 Nginx 配置**:确保 Nginx 的反向代理规则正确,能够将请求正确转发到 uWSGI。例如: ```nginx location / { include uwsgi_params; uwsgi_pass unix:/tmp/myproject.sock; } ``` - **检查文件权限**:确保 Nginx 有足够的权限访问所需的文件和目录。可以使用以下命令修改权限: ```bash sudo chown -R www-data:www-data /path/to/your/project sudo chmod -R 755 /path/to/your/project ``` - **检查日志文件**:查看 Nginx 的错误日志 `/var/log/nginx/error.log` 和访问日志 `/var/log/nginx/access.log`,以获取更多的错误信息[^2]。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值