Python Django开发 异常及其解决办法

1.ValueError: The view *** didn’t return an HttpResponse object. It returned None instead

该错误表明views.py中没有return一个返回值给前端。
解决办法:检查 return HttpResponse()是否错位。

2.NoReverseMatch: Reverse for ‘xxx’ not found . ‘xxx’ is not a valid view function or pattern

在templates里html文件模板用{% url ‘xxx’%}解析时没有跟app_name,具体如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>pagedetail</title>
</head>
<body>
    <div>
        <a href="{% url 'index' %}"> home </a>
    </div>
</body>
</html>

解决办法:
更改HTML文件中url标签部分:
<a href="{% url 'test1_app:index' %}"> home </a>

3.python manage.py migrate,提示No migrations to apply

造成多次应用migrations失败的原因是,当前model是修改过的,原来的migrations已经被删除,但是,重新生成的migrations使用递增整数记名,所以,在django_migrations表中0001,0002等前面几个数字的文件都已被记录,在Django看来,被记录了就相当于已应用,所以,会出现刚开始的No migrations to apply。
解决方案:
python manage.py dbshell 进到数据库中;
执行delete from django_migrations where app='your_appname';
python manage.py makemigrations(若migrations文件未删除,可不执行这一步);
python manage.py migrate即可成功。

发布了55 篇原创文章 · 获赞 194 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/CUFEECR/article/details/104031620