from random import randrange

from paintings.models import Gallery, Painting


def get_Galleries(request):
	return ( {'galleries' : Gallery.objects.all()} )


def get_random_canvasOilPainting(request):
	paintings = Painting.objects.filter(surface='canvas', material='oil').extra(where=['width > height'])
	rand_painting = paintings[ randrange( paintings.__len__() ) ]
	return ( {'rand_painting' : rand_painting} )

from random import randrange

from paintings.models import Gallery, Painting


def get_Galleries(request):
	return ( {'galleries' : Gallery.objects.all()} )


def get_random_canvasOilPainting(request):
	try:
		paintings = Painting.objects.filter(surface='canvas', material='oil').extra(where=['width > height'])
		rand_painting = paintings[ randrange( paintings.__len__() ) ]
	except:
		rand_painting = None
	return ( {'rand_painting' : rand_painting} )

Fix bug with empty db
Fix bug with empty db

  from random import randrange
  
  from paintings.models import Gallery, Painting
  
  
  def get_Galleries(request):
  	return ( {'galleries' : Gallery.objects.all()} )
  
  
  def get_random_canvasOilPainting(request):
+ 	try:
- 	paintings = Painting.objects.filter(surface='canvas', material='oil').extra(where=['width > height'])
+ 		paintings = Painting.objects.filter(surface='canvas', material='oil').extra(where=['width > height'])
- 	rand_painting = paintings[ randrange( paintings.__len__() ) ]
+ 		rand_painting = paintings[ randrange( paintings.__len__() ) ]
+ 	except:
+ 		rand_painting = None
  	return ( {'rand_painting' : rand_painting} )
  
Fix bug with empty db
## Code Before:
from random import randrange

from paintings.models import Gallery, Painting


def get_Galleries(request):
	return ( {'galleries' : Gallery.objects.all()} )


def get_random_canvasOilPainting(request):
	paintings = Painting.objects.filter(surface='canvas', material='oil').extra(where=['width > height'])
	rand_painting = paintings[ randrange( paintings.__len__() ) ]
	return ( {'rand_painting' : rand_painting} )

## Instruction:
Fix bug with empty db
## Code After:
from random import randrange

from paintings.models import Gallery, Painting


def get_Galleries(request):
	return ( {'galleries' : Gallery.objects.all()} )


def get_random_canvasOilPainting(request):
	try:
		paintings = Painting.objects.filter(surface='canvas', material='oil').extra(where=['width > height'])
		rand_painting = paintings[ randrange( paintings.__len__() ) ]
	except:
		rand_painting = None
	return ( {'rand_painting' : rand_painting} )
