错误列表:
1、Invalid block tag on line 13: 'article.title', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
2、{% include "article.html" %}引入页面 为什么内容为空,单独创建一个url 打开就没问题
3、django 多个条件查询 TypeError: Object of type 'QuerySet' is not JSON serializable
4、django 多个条件查询 django.core.exceptions.FieldError: Cannot resolve keyword 'depart' into field. Choices are: author, id, name
5、django 多个条件查询 django.core.exceptions.FieldError: Related Field got invalid lookup: contains
6、module.py 反向关联报错
7、多对多关联 ERRORS:<class 'proApp.admin.AuthorAdmin'>: (admin.E109) The value of 'list_display[8]' must not be a ManyToManyField.
8、通过接口获取数据库数据,某字段是多对多关系,报错TypeError: Object of type 'ManyRelatedManager' is not JSON serializable
9、通过接口获取数据库数据,某字段是一对多关系,报错TypeError: Object of type 'Publisher' is not JSON serializable
10、多对多 修改数据:TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use author.set() instead.
11 、主键由自增改为手动:django.db.utils.InternalError: (1833, "Cannot change column 'id': used in a foreign key constraint 'proApp_author_book_book_id_b4feaab6_fk_proApp_book_id' of table 'python_sql.proapp_author_book'")
1、Invalid block tag on line 13: 'article.title', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
<ul>
{% for article in article_list %}
<li>
<h3>{{ article.title }}</h3>
<p>{{ article.content }}</p>
</li>
{% endfor %}
</ul>
注意:
for循环语句 :{% %}
字段取值: {{ }}
效果图如下所示:
2、{% include "article.html" %}
引入页面 为什么内容为空,单独创建一个url 打开就没问题
def showArticle(request):
from proApp import models
article_list = models.Article.objects.all()
return render(request,"article.html",{"article_list":article_list})
解决办法:
不需要在 def 中return render(返回的是代码片段)。正确代码如下:
def addArticle(request):
title = request.POST.get("title")
content = request.POST.get("content")
dic = {'title': title, 'content': content}
models.Article.objects.create(**dic)
return HttpResponse("文章添加成功!") // 返回的即是ajax 获得data
3、django 多个条件查询
问题1:TypeError: Object of type 'QuerySet' is not JSON serializable
解决办法: https://blog.csdn.net/teavamc/article/details/77777301
问题2:中文获得的是unicode
解决办法:序列化时添加第三个参数 ensure_ascii
articleList = serializers.serialize("json",models.Article.objects.filter(title__contains=title).filter(content__contains=content),ensure_ascii=False)
问题3:ajax获取的list数据 并不是对象形式 https://www.jb51.net/article/136971.htm
解决办法:字符串 -> 字典形式
articleList = serializers.serialize("json",models.Article.objects.filter(title__contains=title).filter(content__contains=content),ensure_ascii=False)
articleList = json.loads(articleList)
data = {
'ret': {
'success': True,
'retCode': 200,
'retMsg': "文章查询成功!"
},
'list': articleList
}
return HttpResponse(json.dumps(data), content_type='application/json; charset=utf-8')
4、django 多个条件查询
问题:django.core.exceptions.FieldError: Cannot resolve keyword 'depart' into field. Choices are: author, id, name
解决办法:
表连错了
5、django 多个条件查询
问题:django.core.exceptions.FieldError: Related Field got invalid lookup: contains
解决办法:contains 是模糊搜索,这里部门值是绝对搜索
depart = request.POST.get("depart")
search_sql = search_sql.filter(name__contains=name).filter(depart=depart)
6、module.py 反向关联报错:
解决办法: 添加 related_name
class Book(models.Model):
author = models.ManyToManyField(Author,null=True,related_name='book_author')
class Author(models.Model):
book = models.ManyToManyField('Book',null=True,related_name='author_book')
7、ERRORS:<class 'proApp.admin.AuthorAdmin'>: (admin.E109) The value of 'list_display[8]' must not be a ManyToManyField.
admin.py: list_display 不可以输出多对多关系表的字段
解决办法:list_display 字段展示上不要加入 多对多关系的字段
8、通过接口获取数据库数据,某字段是多对多关系,报错TypeError: Object of type 'ManyRelatedManager' is not JSON serializable
解决办法:json.loads(serializers.serialize()) 转一下数据类型即可
def getBook(request):
if request.method == "GET":
page = int(request.GET.get('page',''))-1
rows = int(request.GET.get('rows',''))
list = models.Book.objects.all().order_by("-id")
allList = []
for li in list:
author_list = json.loads(serializers.serialize("json", li.author.all(), ensure_ascii=False))
author_name = ''
author_id = []
for a in author_list:
author_id.append(a['pk'])
author_name += a['fields']['name'] + '、'
allList.append({
"id": li.id,
"name": li.name,
"price": li.price,
"saleNum": li.saleNum,
"publisher": li.publisher,
"author_id": author_id,
"author": author_name,
"publish_date": json.loads(json.dumps(li.publish_date, cls=DateEncoder)),
})
total = len(allList)
p = Datagrid()
json_data_list = p.page(page, rows, total, allList)
print (json_data_list)
return HttpResponse(json.dumps(json_data_list), content_type='application/json; charset=utf-8')
9、通过接口获取数据库数据,某字段是一对多关系,报错TypeError: Object of type 'Publisher' is not JSON serializable
解决办法:
数据原始字段:li.publisher, 但是因为是ForeignKey关联其他表所得数据,并且默认外键为id
allList.append({
"id": li.id,
"name": li.name,
"price": li.price,
"saleNum": li.saleNum,
"publisher": li.publisher_id,
"publish_date": json.loads(json.dumps(li.publish_date, cls=DateEncoder)),
})
10、多对多:TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use author.set() instead.
解决办法:
1.getlist()
2.author[]
authors_list = request.POST.getlist("author[]")
11 、主键由自增改为手动:django.db.utils.InternalError: (1833, "Cannot change column 'id': used in a foreign key constraint 'proApp_author_book_book_id_b4feaab6_fk_proApp_book_id' of table 'python_sql.proapp_author_book'")
class Book(models.Model):
id = models.FloatField(primary_key=True)
book = models.Book.objects.all()
if len(book) > 0:
maxId = models.Book.objects.latest('id').id
else:
maxId = 0
maxId += 1
dic = {
'id': maxId,
}
b1 = models.Book(**dic)