简体   繁体   中英

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 .

In Fetching_information_view I am fetching information which I am displaying to the user in the tabular format on the "home.html" page. That's all okay.

Now, I get a CSV URL for each row as well. I don't want that when the user clicks on the CSV URL it should open the 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" .

CSV URL:

/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? It should process in the backend and display results on process.html .

This is the table I am showing on the homepage:

在此处输入图片说明

I've wrote sample code for views.py which I'm using:

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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