Advertisement
Larrisa

File upload error

Aug 24th, 2020
1,516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. views.py
  2.  
  3.     def post(self, request):
  4.         """POST requests"""
  5.         if self.request.POST and self.request.FILES:
  6.             print('picking file ---------')
  7.             csvf = self.request.FILES['file[]']
  8.            
  9.             print('Found file  --',csvf, 'file---------')
  10.             title = self.request.POST['title']
  11.             user = request.user
  12.             product = Product.objects.create(title=title, file=csvf)
  13.            
  14.             if not csvf:
  15.                 raise ValueError('Kindly select a file for upload')
  16.  
  17.             try:
  18.                 with transaction.atomic():
  19.              
  20.                     product.save()
  21.                  
  22.  
  23.                     if user and product:
  24.                         print('processing...... products')
  25.                         async_task('apps.payment.tasks.process_products',
  26.                                    user, product, csvf)
  27.  
  28.  
  29. tasks.py
  30.  
  31. def process_products(user, product, file):
  32.     if user and product:
  33.         reader = csv.reader(file)
  34.             n = next(reader, None)
  35.             print(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement