У меня есть два представления, одно из которых принимает входные данные, а другое - для подтверждения и выполнения действия. Моя проблема заключается в том, как подтвердить входные данные из другого представления и шаблона.
Это похоже на удаление записи. Это вы должны подтвердить от пользователя его действия.
Вот вид ввода. PreprocessinputationView:
def PreprocessInputationView(request, **kwargs):
proj_pk = kwargs.get('pk')
project = Project.objects.get(id=proj_pk)
df = pd.read_csv(project.base_file)
n_cols = df.keys
context = {}
context['df'] = df
context['n_cols'] = n_cols
context['project'] = project
if request.method == 'POST':
# try:
checked_value = request.POST.getlist(u'predictors')
method = ''.join(request.POST.getlist(u'method'))
if checked_value and method:
context['checked_value'] = checked_value
context['method'] = method
return render(request, 'projects/preprocess/confirm_inputation.html', context)
return render(request, 'projects/preprocess/preprocess_inputation.html', context)
Смотри подтверждение. ConfirmInputationView:
def ConfirmInputationView(request, context):
print('method:', context['method'])
project = context['project']
df = pd.read_csv(project.base_file)
n_cols = df.keys
filename = project.base_file.name
tmp = filename.split('/')
filename = str(tmp[1:])
if request.method == 'POST':
# try:
checked_value = context['checked_value']
method = context['method']
if checked_value and (method=='mean'):
df[checked_value].fillna(df[checked_value].mean())
# df.drop(columns=checked_values, inplace=True)
new_df = df.to_csv(index=False)
updated_file = ContentFile(new_df)
updated_file.name = filename
project.base_file = updated_file
project.save()
str_checked_value = ', '.join(checked_value)
context['str_checked_value'] = str_checked_value
if str_checked_value:
messages.success(request, f'Inputation to column(s) {str_checked_value} successful!')
return render(request, 'projects/preprocess/preprocess_inputation.html', context)
Шаблон подтверждения. Confirm_inputation.html:
{% extends "base.html" %}
{% block page_heading %}
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Delete Project</h1>
</div>
{% endblock page_heading %}
{% block content %}
<div class="jumbotron col-xl-8 col-md-6 mb-1"">
<form method=" POST">
{% csrf_token %}
<fieldset class='form-group'>
<p>
You have chosen <strong>{{ method }}</strong> as an inputation method?
Are you sure you want to proceed?
</p>
</fieldset>
<div class="form-group">
<button class="btn btn-danger float-sm-right mr-1" type="submit">Yes, Delete</button>
<a class="btn btn-secondary float-sm-right mr-1" href="{% url 'project-detail' project.id %}">Cancel</a>
</div>
</form>
</div>
{% endblock content %}
Данные из PreprocessImputationView
должны быть переданы в ConfirmImputationView
для подтверждения и обработки.