将上下文从一个视图传递到另一个视图

时间:2019-04-07 13:44:48

标签: django

我有两个视图,一个视图接受输入,另一个视图用于确认和执行动作。我的问题是,在确认视图中似乎无法访问第一个视图中的上下文数据。

这是输入视图。 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 %}

确认归因后,程序应该能够执行df[checked_value].fillna(df[checked_value].mean())内部的代码if request.method == 'POST':

1 个答案:

答案 0 :(得分:0)

您应该返回视图功能,而不是在class Player { //... std::vector<std::unique_ptr<Building>> buildingCards; friend static void swapBuildingCards(Player& p1, Player &p2) { using std::swap; swap(p1.buildingCards,p2.buildingCards); } }; 视图中呈现确认模板。也就是说,替换

PreprocessInputationView

使用

return render(request, 'projects/preprocess/confirm_inputation.html', context)