Django - 将另一个函数中的数据传递给html

时间:2017-08-24 02:42:20

标签: python html django

所以我将一些数据从views.py传递给list.html(仅显示list.html中的id)

我想将所选内容的数据传递给detail.html,以便用户查看详细信息。

我应该在return的{​​{1}}写一下什么?

这是代码的一部分。 非常感谢!

views.py

analysis_detail

urls.py

  def analysis_list(request):
        return render(request, 'analysis/list.html', {
            "analysis_list": [
                {
                    "id": "1",
                    "sample_name": "samplefile1",
                },
                {
                    "id": "2",
                    "sample_name": "samplefile2",
                },
                {
                    "id": "3",
                    "sample_name": "samplefile3",
                },
                {
                    "id": "4",
                    "sample_name": "samplefile4",           
                }
            ]
        })


def analysis_detail(request, analysis_id):

    return render(request, 'analysis/detail.html', {})

1 个答案:

答案 0 :(得分:0)

views.py

def analysis_list(self, request, *args, **kwargs):
    if 'analysis_id' in kwargs:
        analysis_id = kwargs['analysis_id'] #here you get your selected id
    return render(request, 'analysis/list.html', {
        "analysis_list": [
            {
                "id": "1",
                "sample_name": "samplefile1",
            },
            {
                "id": "2",
                "sample_name": "samplefile2",
            },
            {
                "id": "3",
                "sample_name": "samplefile3",
            },
            {
                "id": "4",
                "sample_name": "samplefile4",           
            }
        ]
    })


def analysis_detail(self, request, *args, **kwargs, analysis_id):
   kwargs['analysis_id']=analysis_id
   return self.analysis_list(request, *args, **kwargs)

希望它能帮到你