单击链接按钮后如何删除数据框列?

时间:2019-04-02 14:02:28

标签: django

我正在尝试删除特定的数据框列。我的问题是,如何从模板中调用函数,并在删除列后重新呈现相同的模板。

我尝试创建一个函数来处理视图中列的删除,但无法弄清楚如何实际执行该函数。执行该功能后,应刷新模板以反映更新的页面...

这是我的模板,在其中选择要删除的列(preprocess.html):

 <tbody>
                {% for key in n_cols %}
                <tr>
                    <td>{{ key }}</td>
                    <td align="right">{% get_key_count df key %}</td>
                    <td align="right">{% get_missing df key %}</td>
                    <td align="right">{% get_missing_rows_percent df key %}</td>
                    <td class="text-center">
                        <a href="#" class="btn btn-info btn-sm">
                            <i class="fas fa fa-pencil fa-cog"></i>
                        </a>
                    </td>

                    <td align="right" class="text-center">
                        <a href="{% delRowsMissingView project.id df key %}"
                           class="btn btn-warning btn-sm">
                        <i class="fas fa-trash fa-cog"></i>
                        </a>
                    </td>
                    <td class="text-center">
                        <a href="#" data-q_id="{{ key }}"
                            class="btn btn-danger btn-sm" 
                            data-toggle="modal" 
                            data-target="#delMissingColModal">
                            <i class="fas fa-trash fa-cog"></i>
                        </a>
                    </td>
                </tr>

以下视图有助于呈现模板:

class PreprocessView(DetailView):
    model = Project
    context_object_name = 'projects'
    template_name = 'projects/preprocess/preprocess.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        project = self.get_object()

        try:
            df = pd.read_csv(project.base_file)
        except: pass

        context['project'] = project
        context['df']      = df
        context['n_cols']  = df.keys

        return context

     def drop_row(df, key):
        df[key].dropna()

基础模型:

class ProjectFile(models.Model):
    FILE_TYPE = (
        ('base', 'Base File'),
        ('transformed', 'Transformed File'),
        ('discretized', 'Discretized File'),
        ('model', 'Model File')
    )

    file = models.FileField(upload_to='project-files', default=True)
    category = models.CharField(max_length = 50, default = 'base', choices = FILE_TYPE)
    description = models.TextField(default = 'base file')
    project = models.ForeignKey(Project, on_delete=models.CASCADE)

    def __str__(self):
        return self.description

    def get_absolute_url(self):
        return reverse('project-detail', kwargs = {'pk': self.project.pk})

0 个答案:

没有答案