-1

Thanks for the first feedback, it's seems that my explanations are too confused, I add some code block and pictures to illustrate it.

So you can fill a form with a domain list on a csv file. form with csv input

Domains from the list are analysed and passed to the multiresult view:

def multiresult(request, *args, **kwargs):
    input_file = request.FILES['inputFile']

    def several_scoring(domain_file):
        domain_name_list = []
        line = domain_file.readline().decode("utf-8")
        while line != "":
            domain_name_list.append(line)
            line = domain_file.readline().decode("utf-8")

        domain_list = []
        for line in domain_name_list:
            domain = Domain(line.rstrip())
            domain.start_scanning()
            domain_list.append(domain)

        return domain_list

    domain_list = several_scoring(input_file)
    headers = domain_list[0].header_grading.displayable_list
    context = {
        'domain_list': domain_list,
        'headers': headers,
        'domain_list_dict': json.dumps([domain.__as_dict__() for domain in domain_list])
    }
    return render(request, "multiresult.html", context)

On the template I put a download button to save those results on a generated csv file throught a download view: multiresults page

Currently the download view get domain_list_dict thanks to the url:

<a href="{% url 'domain_analyse:download' domain_list=domain_list_dict %}" method="post" >
    <button type="button" class="btn btn-info btn-lg">
        <i class="fa fa-download"></i> Télécharger les résultats
    </button>
</a>

The download view is able to generate a csv file without problems when it gets data.

My question is how can I pass the domain_list_dict (which is in json format) to my download view without using url ? Like django automatically does with forms I wanted to change the request.POST but I don't really know how to do this. Should I use javascript in client side or is it possible to do this with jinja or django mechanism ?

If information are still missing let me know.

Thanks !

Léo Mouyna
  • 25
  • 1
  • 8
  • 2
    It is very unclear what you are asking. You should add some of your code and explain the problem refering to it. – Klaus D. Jul 12 '18 at 09:37
  • But if the user makes a POST request, you can store information in the `request.POST`. The `request.POST` is immutable *at the server side*. – Willem Van Onsem Jul 12 '18 at 09:37
  • Nevertheless, the description is a bit "*chaotic*". Can you provide some MWE of a sample program that would for example construct a response, such that it is clear what you aim to do? – Willem Van Onsem Jul 12 '18 at 09:40
  • json-dump it to str to the template context and in client side json-load it? so you can keep data model and use it however you want in the browser. – panchicore Jul 12 '18 at 09:43

1 Answers1

0

If I understood correctly, you want your users to be able to download a file (.csv) you generated after the user filled your form. Nonetheless, you don't want to store the data on a database, or locally. Is that it?

In other words, what I'm imagining is you having a page with the generated table and a button saying "Download CSV", or something like that. What you could do is to create an URL to download that .csv, generate it and in your response use the "Content-Disposition" header. Just like this answer: https://stackoverflow.com/a/37299531/9989707

  • I updated my first post. @Bruno Gonçalves you are right. But your linked question doesn't solve my problem. I don't know how to change the post request from the client side. – Léo Mouyna Jul 19 '18 at 13:51