'NoneType'对象没有属性'user' [英] 'NoneType' object has no attribute 'user'
本文介绍了'NoneType'对象没有属性'user'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
models.py
models.py
class Report(models.Model):
user = models.ForeignKey(User, null=False)
photo_files_attached = models.BooleanField('Photos', default=False)
views.py
def media(request):
user = request.user
try:
report = Report.objects.get(user=user.id)
except:
report = None
report_dir = str(report.user.id) + '/' + str(report.id)
output_dir = settings.MEDIA_ROOT + '/' + report_dir
imagelist = []
if os.path.exists(output_dir):
os.chdir(output_dir)
for files in os.listdir("."):
for extension in JPEG_ALLOWED:
if files.endswith(extension):
image_with_path = report_dir + '/' + files
imagelist.append(image_with_path)
return render(request, 'incident/media.html',
{
'newreport_menu': True,
'report':report,
})
我正在尝试上传图像文件并将其保存到数据库,得到以下错误'NoneType'对象没有属性'user'
I am trying to upload the image file and save it to database,getting the below error "'NoneType' object has no attribute 'user'
"
推荐答案
似乎
Report.objects.get(user=user.id)
给你一个 EmptyQuerySet
有问题的用户可能没有任何关联的报告
实例。
The user in question probably hasn't got any associated Report
instances yet.
您可以使用 get_or_create
这篇关于'NoneType'对象没有属性'user'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文