kusut
kusut

Reputation: 1640

Checking FieldFile on django form

beginner question. here's pdb output

(Pdb) entry.image
<FieldFile: None>
(Pdb) entry.image is None
False

how do I check if the image exists or not?

Lets say I want to access entry.image.file but I dont know whether entry.image exists

SOLVED check my own answer

Upvotes: 1

Views: 622

Answers (3)

kusut
kusut

Reputation: 1640

Found out how to do it

bool(entry.image)

Upvotes: 1

Manoj Govindan
Manoj Govindan

Reputation: 74705

How about:

import os
os.path.exists(file_path) and os.path.isfile(file_path)

Upvotes: 0

lprsd
lprsd

Reputation: 87095

Here:

file_path = entry.image.path
open(file_path)

If the file exists, there should not be any error.

Upvotes: 0

Related Questions