Answer the question
In order to leave comments, you need to log in
How to catch file from file input field and put into variable in Django function?
There is such a page. It is necessary that on pressing the button to execute, a variable containing the file from the input file field is created in the show_data function. You don't need to upload the file to the Django server. This functionality has already been implemented. The bottom line is to get exactly the file from the input into the variable on this page. I hope I explained clearly. In a little more detail: I'm checking an excel file with another excel file. The functionality of all checks is implemented. But the second check file is stupidly loaded from the desktop, and the other file is already taken from the database. For example, I inserted the "Graphics" check function at the end
{% extends "base.html" %}
{% block title %}Проверка плана{% endblock title %}
{% block content %}
<div class="container" style="margin-top: 80px" >
<form enctype="multipart/form-data" action="" method="POST">
{% csrf_token %}
<br style="margin-top: 5px">
<h4>Выберите шаблон .xlsx:</h4>
<input style="" type="file" name="shablon" value="shablon" id="shablon">
<br>
<h4>Выберите параметры проверки:</h4>
<div>
<input type="checkbox" id="titul" name="titul" align="left">
<label for="scales">Титульный лист</label>
</div>
<div>
<input type="checkbox" id="grafik" name="grafik" align="left">
<label for="horns">График</label>
</div>
<div>
<input type="checkbox" id="plan" name="plan">
<label for="horns">План</label>
</div>
<div>
<input type="checkbox" id="plan_svod" name="plan_svod">
<label for="horns">План/Свод</label>
</div>
<div>
<input type="checkbox" id="comp" name="comp">
<label for="horns">Компетенции</label>
</div>
<div>
<input type="checkbox" id="All" name="all">
<label for="horns">Весь план</label>
</div>
<div>
<input type="checkbox" id="edit" name="edit">
<label for="horns">Заменить ошибочные данные</label>
</div>
<input type="submit" name="go" value="Выполнить" class="btn btn-primary"><br></br>
<a href="{% url "show_files" %}" class="btn btn-secondary"> На главную </a><br>
</form>
</div>
{% endblock content %}
def show_data(req, file_id):
file = Files.objects.get(file_id=file_id)
if req.method == "GET":
return render(req, "show_data.html", locals())
if req.method == "POST":
if 'titul' in req.POST:
file = Files.objects.get(file_id=file_id)
check_titul(req, file)
file = Files.objects.get(file_id=file_id)
if 'all' in req.POST:
check_all(req, file)
file = Files.objects.get(file_id=file_id)
if 'grafik' in req.POST:
file = Files.objects.get(file_id=file_id)
check_grafik(req, file)
file = Files.objects.get(file_id=file_id)
if 'plan' in req.POST:
file = Files.objects.get(file_id=file_id)
check_plan(req, file)
file = Files.objects.get(file_id=file_id)
if 'plan_svod' in req.POST:
file = Files.objects.get(file_id=file_id)
check_plan_svod(req, file)
file = Files.objects.get(file_id=file_id)
if 'comp' in req.POST:
file = Files.objects.get(file_id=file_id)
check_compititions(req, file)
file = Files.objects.get(file_id=file_id)
return render(req, "show_data.html", locals())
else:
return render(req, "show_data.html", locals())
def check_grafik(req,file):
wb = open_wb(file)
sheet_wb = wb['График']
shablon = openpyxl.load_workbook('C:/Users/user/Desktop/check.xlsx')
sheet_shablon = shablon['График']
name_file = give_name_file(file)
row_count = sheet_shablon.max_row
a = find_index(a='I', sheet = sheet_shablon, count=row_count) - 2
b = find_index(a='IV', sheet = sheet_shablon, count=row_count) + 6
list = give_list(2, 54, sheet = sheet_shablon, sheet2=sheet_wb, m=a, n=b)
wb.save('media/' + 'media/' + name_file + '.xlsx')
wb.close()
return locals()
Answer the question
In order to leave comments, you need to log in
def check_grafik(req,file,shab):
wb = open_wb(file)
sheet_wb = wb['График']
shablon = load_workbook(shab)
sheet_shablon = shablon['График']
name_file = give_name_file(file)
row_count = sheet_shablon.max_row
a = find_index(a='I', sheet = sheet_shablon, count=row_count) - 2
b = find_index(a='IV', sheet = sheet_shablon, count=row_count) + 6
list = give_list(2, 54, sheet = sheet_shablon, sheet2=sheet_wb, m=a, n=b)
wb.save('media/' + 'media/' + name_file + '.xlsx')
wb.close()
return locals()
if 'grafik' in req.POST:
file = Files.objects.get(file_id=file_id)
check_grafik(req, file,shab=req.FILES['shablon'])
https://docs.djangoproject.com/en/2.2/topics/http/...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question