django - 'NoneType' object has no attribute 'user' -


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)         files in os.listdir("."):             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,                   })    

i trying upload image file , save database,getting below error "'nonetype' object has no attribute 'user'"

it seems

report.objects.get(user=user.id) 

gives emptyqueryset

the user in question hasn't got associated report instances yet.

you use get_or_create


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -