-1

I'm creating a list in a view to show elements from loaded csv file(def showCsv(request)). What I would like to get this list and put this list in url redirection (the first link 'a'), but I don't know how to do this.

def showCsv(request):
    if request.POST:
       object_pk = request.POST.get('pk')
       csvname = request.FILES['f']
       path = default_storage.save('media/csv', ContentFile(csvname.read()))
       tmp_file = os.path.join('', path)
       csvname = open(tmp_file, 'rb')

       reader = csv.reader(codecs.iterdecode(csvname, 'utf-8'), delimiter=";")
       list_test = []
       for row in reader:
           list_test.append((row))

    return render(request, 'website/show_csv.html',
    { 'app_version':APP_VERSION, 'object_pk':object_pk, 'list': list_test})

show_csv.html

{% block content %}
<div class="menu">
 <ul class="blockchoixcontinuer">
   <a href="{% url 'website:impression_publipostage' object_pk list %}"> Continuer </a>
 </ul>
  <ul class="blockchoixretour">
    <a href="{% url 'website:addcsv' object_pk %}"> Retour </a>
  </ul>
 </div>
 <div class="container" id="container">
 <div class="row">
  <table class="table table-striped col-md-12">
      <tbody>
            {% for row in list %}
              <tr>
                  <td>{{ row.0 }}</td>
                  <td>{{ row.1 }}</td>
              </tr>
            {% endfor %}
        </tbody>
    </table>
  </div>
 </div>
{% endblock %}
Jerzyk
  • 3,662
  • 23
  • 40
Nosmoz RG
  • 15
  • 3
  • Can't you store that CSV in a model? If yes, you can write a `view` which displays this list with a refering `pk`. – Maxime B Jun 09 '16 at 13:20
  • I can't use model because i didn't to store element of my csv, because when user log on my website, the csv is just "load temporarly" on media. – Nosmoz RG Jun 09 '16 at 13:25
  • What to you clearly want to do with this list and URL ? – Maxime B Jun 09 '16 at 13:27
  • For example, they are elements on my csv : [['NumeroProduit', 'NumeroSerie'], ['SUPP-MC67-01110', '20160428'], ['PACE-MK25-03265', '10035701']], and i'd like to save or transfert this list on another view to use elements to write templates and print them. I choice url because i can get the list (like get_object() for pk) – Nosmoz RG Jun 09 '16 at 13:36
  • Ok, what's the `pk` refering to? – Maxime B Jun 09 '16 at 13:46
  • User choice a label format to print (the pk is the id of label), after he choice a type of printing and if he choice the the second method, he load his csv, show his csv and print his label. My problem his on show csv, dunno how get the list. – Nosmoz RG Jun 09 '16 at 13:55
  • Since you're storing it in `media`, can't you create a `FileField` in this model refering to this field? If not tell me clearly why is it different from your method atm. If you can then i've an answer. – Maxime B Jun 09 '16 at 13:57

2 Answers2

0

if I'm understanding this, you are reading a list in one view (where you get it from uploaded file) and then you would like to pass whole list to the other view.

for sure you will not be able to pass whole list as url parameters - if file will be bigger than allowed url length - it will fail. Other issues are matter of performance or even - clarity.

There are multiple solutions for passing data between views, but you need to store and keep data somehow on the server side, and then pass some identifier between views.

simplest one is to use database, more complex - store data into file (you have this file already), then store path to the file in session or pass it via parameter to the url, you can store data in the cache (if it is not so big) etc.

Jerzyk
  • 3,662
  • 23
  • 40
0

Sorry for my absence,I found a solution to my question.

To do this i write the url of my csv in other file and i open this file on my other view, not really the best idea but it's work. After you just have to work on (add element, delete element, show in context,...). Thanks for your help.

ShowCsvView
path = default_storage.save('media/csv', ContentFile(csvname.read()))
    tmp_file = os.path.join('', path)
    csvname = open(tmp_file, 'rb')

    reader = csv.reader(codecs.iterdecode(csvname, 'utf-8'), delimiter=";")
    list_test = []
    for row in reader:
        list_test.append((row))

    f = io.open("media/list_file", "w")
    f.write(tmp_file)
    f.close()

CreatePdfView
list_element_csv = []
    with io.open("media/list_file", "r") as infile:
        str_path = infile.read()

    csvname = open(str_path, 'rb')
    reader = csv.reader(codecs.iterdecode(csvname, 'utf-8'), delimiter=";")
    for row in reader:
        list_element_csv.append(row)
Nosmoz RG
  • 15
  • 3