Django视图设计-从用户生成的内容导出CSV [英] Django view design - export CSV from user-generated content

查看:55
本文介绍了Django视图设计-从用户生成的内容导出CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Django应用中,用户为每个请求获得唯一(部分随机)的输出.我想允许用户下载与CSV完全相同的输出(显示在HTML表格中).

On my Django app, users get a unique (partly random) output for each request. I would like to allow the user to download the exact same output (shown in an HTML table) as CSV.

我的难题是如何设计此流程,以便将从 form.generate_cassettes()生成的结果用于csv导出.

My difficulty is how to design this flow so the result generated from form.generate_cassettes() will be used for the csv export.

导出逻辑应该去哪里?在我看来,csv响应应位于何处?HTML页面中的导出到csv"按钮应该调用什么?

Where should the export logic go? Where should the csv response be located in my view? What should the "export to csv" button in the HTML page be calling?

这是我的Django应用程序中的视图:

This is the view in my django app:

def generate_rna(request):
if request.method == 'POST':
    form = RNAGeneratorForm(request.POST)

    if form.is_valid():
        result = form.generate_cassettes()
        context = {
            "form": form,
            "cassettes": result
        }

        return render(request, 'rnagen/RNA.html', context)

else:
    form = RNAGeneratorForm()

context = {
    "form": form
}

return render(request, 'rnagen/RNA.html', context)

"cassettes":结果和和输入形式(字段名称 string )是生成CSV所需的全部信息.

The "cassettes": result and the and the input form (field name string) is all the information I need to generate the csv.

推荐答案

  • 盒式磁带是一个包含Django模型对象 sequence

    • cassettes is an object that contains django model objects sequence

      序列具有解析所需的自己的get_type和score属性:

      sequence has it's own get_type and score attribute needed to parse:

          class Cassette:
          def __init__(self):
              self.sequences = []  # list of BaseSequences
              self.score = 0.0     # total cassette score
              self.scores = {}     # score per protein
      
          def add(self, sequence):
              self.sequences.append(sequence)
              self.score += sequence.score
      
              if isinstance(sequence, Binding):
                  sequence_type = sequence.get_type()
                  self.scores[sequence_type] = self.scores.get(sequence_type, 0) + sequence.score
      
          def add_list(self, sequence_list):
              for s in sequence_list:
                  self.add(s)
      
      

      所以我得到 Cassette类型的对象不是JSON可序列化错误.

      我试图找到一种方法,否在客户端将对象序列化,并在视图上反序列化.

      I am trying to find a way no to seralize this object on client-side and deserialize it on the views.

      这篇关于Django视图设计-从用户生成的内容导出CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆