Django Response Expected A `Response`, `HttpResponse` Or `HttpStreamingResponse` To Be Returned From The View, But Received A `
my model has attributes fields as file and the person who uploaded it ,in my case i would want to show the error no file found on my website front end when there is no data in model but instead it is taking me to this error i have implemented my code like this
@api_view(('GET',)) def template_view(request): data = Mymodel.objects.first() try: if data: response = HttpResponse( data.file, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, ' 'application/vnd.ms-excel', ) return response except FileNotFoundError: raise ValueError('No file found')
Perhaps instead of re-raising a ValueError, you can return a 400 error e.g.
@api_view(('GET',)) def template_view(request): data = Mymodel.objects.first() try: response = HttpResponse( data.file, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, ' 'application/vnd.ms-excel', ) return response except (FileNotFoundError, AttributeError): return HttpResponse("No File Found", status=400)
I'd also catch the AttributeError rather than put an if statement for the data
- → How to configure JSON for graphql query?
- → Google couldn't fetch my sitemap.xml file
- → A lot of socket endpoints in python?
- → Historical price per minute between two timestamps for a cryptocurrency
- → How to get a value from a list of dictionaries - Python 3.7.1
- → What is the optimal way to parse these strings in Python?
- → Short Order on Binance futures testnet resulting in APIError (ReduceOnly Order is Rejected)
- → values subtracted while iterating through list has random miscalculations
- → Foreign key query error in case of custom uint64 field which was used as a primary key
- → Grouping all tests Python
- → Using #!python2 not working to run under Python 2
- → Storing last 3 scores and deleting older scores and calculating average?
- → Checking if input is in a list of numbers in python