Reputation: 6657
I use django to build my web app. And one of the page sends different files (with different file types). I have 6 file type. And my current version of code (it's really bad - that is why I write this question) is:
try:
file = request.FILES[u'file_doc']
except MultiValueDictKeyError:
try:
file = request.FILES[u'file_fb2']
except MultiValueDictKeyError:
try:
file = request.FILES[u'file_pdf']
except MultiValueDictKeyError:
try:
file = request.FILES[u'file_txt']
except MultiValueDictKeyError:
try:
file = request.FILES[u'file_other']
except MultiValueDictKeyError:
try:
file = request.FILES[u'file_chm']
except MultiValueDictKeyError:
return HttpResponse('bad file type')
Could you advise me - how to improve this bad peace of code.
TIA!
Upvotes: 0
Views: 575
Reputation: 1124110
The in
test let's you see if a key is part of a dictionary.
if u'file_doc' in request.FILES:
file = request.FILES[u'file_doc']
You can simply loop through a set of keys to test for:
for key in (u'file_doc', u'file_fb2', .. ):
if key in request.FILES:
file = request.FILES[key]
break
else:
return HttpResponse('bad file type')
The else
suite will only be executed if the for loop completed without reaching a break
statement, i.e. you didn't find a matching key.
Note that you could still use your exception approach in the loop:
for key in (u'file_doc', u'file_fb2', .. ):
try:
file = request.FILES[key]
break
except MultiValueDictKeyError:
pass
else:
return HttpResponse('bad file type')
but I am not so sure that is more readable.
Upvotes: 6
Reputation: 114038
def GetFile(request):
file_types = "doc fb2 pdf txt other chm".split()
for k in file_types:
if "file_{0}".format(k) in request.FILES:
return request.FILES["file_{0}".format(k)]
at least thats how I would do it
Upvotes: 1
Reputation: 312370
You could do something like this:
filetypes = [ u'file_doc', u'file_fb2', u'file_pdf' ]
file = None
for ft in filetypes:
if ft in request.FILES:
file = request.FILES[ft]
break
if file is None:
return HttpResponse('bad file type')
Upvotes: 3
Reputation: 578
request.FILES.get(u'file_doc', DEFAULT_VALUE)
will return DEFAULT_VALUE if the key does not exist in the dictionary. You can also use the in
keyword as mentioned by Martijn
edit see also django MultiValueDictKeyError error, how do i deal with it
Upvotes: 2