简体   繁体   English

Django:在View中传递参数并显示在页面上

[英]Django: Pass arguments in View and display on the page

Say, I've got 2 views in views.py : Fetching_information_view and Processing_view . 说,我在views.py有2个视图: Fetching_information_viewProcessing_view

In Fetching_information_view I am fetching information which I am displaying to the user in the tabular format on the "home.html" page. Fetching_information_view我正在获取要在"home.html"页面上以表格格式显示给用户的信息。 That's all okay. 没关系的

Now, I get a CSV URL for each row as well. 现在,我也为每行获得一个CSV URL。 I don't want that when the user clicks on the CSV URL it should open the CSV; 我不希望用户单击CSV URL时应打开CSV。 instead, when the user clicks on it then it should go to the Processing_view and it should be served on a different HTML page, say "process.html" . 相反,当用户单击它时,它应该转到Processing_view ,并且应该在另一个HTML页面(例如"process.html"上进行投放。

CSV URL: CSV网址:

/some_bucket/some_csv_file.csv?AWSAccessKeyId=some_id&Expires=1234&Signature=some_signature

Desired URL: 所需网址:

http://example.com/process.html?file=some_bucket/some_csv_file.csv?AWSAccessKeyId=some_id&Expires=1234&Signature=some_signature

Now, how can I call Processing_view from the Fetching_information_view view and send the file information? 现在,如何从Fetching_information_view视图调用Processing_view并发送文件信息? It should process in the backend and display results on process.html . 它应该在后端处理并在process.html上显示结果。

This is the table I am showing on the homepage: 这是我在首页上显示的表格:

在此处输入图片说明

I've wrote sample code for views.py which I'm using: 我已经为views.py使用了示例代码:

def process_data(request):
    # what should come here?
    # Display in process.html


def home(request):
    some_data = SomeTable.objects.filter(user = request.user)
    args = {"some_data": some_data}
    # display as table on home.html, including URLs it is carrying
    return render(request, "home.html", args)

If you don't want to expose the URL of your CSV files, then don't add it (and also don't expose the URL parameters) to the context of your home view and template. 如果您不想公开CSV文件的URL,则不要将其添加(也不要公开URL参数)到home视图和模板的上下文中。

So assuming you've created a urlpattern named process_view for your process_data view, in your home.html template you can just use href="{{% url 'process_view' %}?id={{ id }}" to append the id of the object to your request's URL parameters. 因此,假设您已经为process_data视图创建了一个名为process_view的urlpattern,那么在home.html模板中,您可以仅使用href="{{% url 'process_view' %}?id={{ id }}"来添加id对象到您请求的URL参数中。

Then in the process_data view, you can get this id by id = request.GET.get('id') , fetch the model with this id and reconstruct the CSV url and file parameters. 然后在process_data视图中,可以得到这个idid = request.GET.get('id')取得与该模型id和重建的CSV URL和文件参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM