django 配置404页面
时间: 2023-10-05 20:11:46 浏览: 144
在 Django 中,可以通过以下步骤配置 404 页面:
1. 在项目的根目录下创建一个名为 `templates` 的文件夹,如果已经存在则跳过此步骤。
2. 在 `templates` 文件夹下创建一个名为 `404.html` 的文件,该文件将作为 404 页面的模板。
3. 在项目的 `urls.py` 文件中添加以下代码:
```python
handler404 = 'views.handler404'
```
4. 在项目的 `views.py` 文件中添加 `handler404` 函数:
```python
from django.shortcuts import render
def handler404(request, exception):
return render(request, '404.html', status=404)
```
现在,当用户访问不存在的页面时,将显示 404 页面。注意,如果你使用的是 Django 2.0 或更早版本,需要将 `handler404` 函数的参数从 `exception` 改为 `request`。
阅读全文
相关推荐


















