Когда я запускаю свой код, я получаю следующие ошибки:
if not csv_file.name.endswith('.csv'):
UnboundLocalError: локальная переменная 'csv_file', на которую ссылается перед присваиванием
views.py
import csv, io
from django.shortcuts import render
from django.contrib import messages
from .models import Profile
# Create your views here.# one parameter named request
def profile_upload(request): # declaring template
template = "profile_upload.html"
data = Profile.objects.all()# prompt is a context variable that can have different values depending on their context
prompt = {
'order': 'Order of the CSV should be product_title, sku, slug, image_path, price',
'profiles': data
}
# GET request returns the value of the data with the specified key.
if request.method == "GET":
return render(request, template, prompt)
csv_file = request.FILES['file'] # let's check if it is a csv file
if not csv_file.name.endswith('.csv'):
messages.error(request, 'THIS IS NOT A CSV FILE')
data_set = csv_file.read().decode('UTF-8') # setup a stream which is when we loop through each line we are able to handle a data in a stream
io_string = io.StringIO(data_set)
next(io_string)
for column in csv.reader(io_string, delimiter=',', quotechar="|"):
_, created = Profile.objects.update_or_create(
name=column[0],
email=column[1],
address=column[2],
phone=column[3],
profile=column[4]
)
context = {}
return render(request, template, context)
Я не уверен, почему я получаю эту ошибку, было бы здорово, если бы кто-нибудь мог помочь мне понять, что я делаю неправильно.