0

my task is : upload a CSV file in Django model my model.py is given below

from django.db import models

# Create your models here.
class Chart(models.Model):
    date=models.DateTimeField(blank=True,null=True)
    open=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    high=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    low=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    close=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)

    def __str__(self):
        return self.str(date)
class NSEBHAV(models.Model):
    symbol=models.CharField(max_length=20,null=True,blank=True)
    series=models.CharField(max_length=2,null=True,blank=True)
    open=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    high=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    low=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    close=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    last=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    prev_close=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    tottrdqty=models.IntegerField(null=True,blank=True)
    tottrdval=models.DecimalField(max_digits=10,decimal_places=3,null=True,blank=True)
    timestamp=models.DateTimeField(blank=True,null=True)
    totaltrades=models.CharField(max_length=20,null=True,blank=True)
    isin=models.CharField(max_length=20,null=True,blank=True)

    def __str__(self):
        return self.symbol

my view.py is given below

import csv,io
from django.shortcuts import render
from django.contrib import messages
from .models import NSEBHAV,Chart
# Create your views here.
def upload_nse(request):
    template='upload.html'
    data=NSEBHAV.objects.all()
    prompt={
        'order': 'Order of the CSV should be (symbol,series,open,high,low,close,last,prevclose,tottrdqty,tottrdval,timestamp,totaltrades,isin',
        'profiles': data   
    }
    if request.method=='GET':
        return render(request,template,prompt)
    csv_file=request.FILES['file']
    print(csv_file)
    # if not csv_file.name.endwith('.csv'):
    #     messages.error(request,'This is not csv file')
    data_set=csv_file.read().encode('utf-8')
    io_string=io.StringIO(data_set)
    next(io_string)
    for column in csv.reader(io_string,delimiter=',',quotechar="|"):
         _, created = NSEBHAV.objects.update_or_create(
        symbol=column[0],
        series=column[1],
        open=column[2],
        high=column[3],
        low=column[4],
        close=column[5],
        last=column[6],
        prevclose=column[7],
        tottrdqty=column[8],
        tottrdval=column[9],
        timestamp=column[10],
        totaltrades=column[11],
        isin=column[12]
    )
    context = {}
    return render(request, template, context)

csv.file sample is enter image description here

error screenshot enter image description here

my html file is is given below: thanks in advance . if i use encode is is giving same error and also i want to check type of uploaded file is csv or not

1 Answer 1

1

The tutorial does not use encode. The code is:

data_set = csv_file.read().decode("UTF-8")
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it is work but i don't know why it was not working before because i use decode first when it not work i use encode ,can you help me why 'str' object has no attribute 'endwith' is givven by same code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.