1、遇到This field is requierd
錯(cuò)誤
如圖示
if request.method == "POST":
form = MyinfoForm(request.POST, request.FILES)
print(request.POST)
使用request.FILES
時(shí)霍转,出現(xiàn)This field is requierd
錯(cuò)誤
解決辦法:form里面加個(gè)字段 required=False
2、表單無(wú)法傳遞變量的問(wèn)題
Django使用POST提交profile_image后很泊,但無(wú)法打印內(nèi)容,怎么回事委造?
解決辦法:
頁(yè)面模板中form表單加 enctype="multipart/form-data" 這個(gè)屬性
3均驶、
ForeignKey
的交叉查詢(xún)在數(shù)據(jù)模型中,若有ForeignKey妇穴,想交叉查詢(xún)內(nèi)容。
解決辦法:
例如Models.py中定義如下內(nèi)容
class A(models.Model):
name = models.CharField(u'名稱(chēng)')
class B(models.Model):
aa = models.ForeignKey(to=A,related_name="FAN")
bb = models.CharField(u'名稱(chēng)')
查A(以B為過(guò)濾條件): A.objects.filter(FAN__bb='XXXX')腾它,都知道related_name的作用,A.FAN.all()是一組以A為外鍵的B實(shí)例瞒滴,可前面這樣的用法是查詢(xún)出所有(B.aa=A且B.bb=XXXX)的A實(shí)例,然后還可以通過(guò)__各種關(guān)系查找妓忍。