title: Save A Matplotlib Figure to A File Along with Displaying it on The Browser Django
body: <p>I am very new to Django and the following is a detailed explanation of the problem I am trying to resolve using Django Python. </p>
  
  <p><strong>Problem:-</strong></p>
  
  <p>Upload a multiple csv files (<strong>five files total</strong>) and then take those files and construct five figures corresponding to the uploaded files. Finally, I want to have a single page in which I ask the user to upload the files and then the user will get a <strong>conformational message for files uploaded successfully</strong>. After that, the files should be displayed only if the user hit the graphing button and should display the five figures on the same webpage. </p>
  
  <p>My attempt thus far:-</p>
  
  <p>A snippet of My views.py:</p>
  
  <pre><code>def Upload(request):
    list_doc = []
    for count, x in enumerate(request.FILES.getlist('files')):
               def multifiles(f):
                      with open(dir_path + x.name + str(count) + ".csv" , 'wb+') as destination:
                              list_doc.append(x.name)
                              for chunk in f.chunks():
                                      destination.write(chunk)
               multifiles(x)
  
    docs = list_doc
    return render_to_response("myapp/mypage.html", {'upload_num':str(count+1)+" Files uploaded successfully!", "files_list":docs}, context_instance = RequestContext(request))
  
  
  def graph(request):
  
   plt.figure(num)
   plt.plot(x,y, color =colorcurve, label=filename)
   ax = plt.subplot(111)
   ax.plot(x,y, color =colorcurve, linewidth=1.5)
   plt.legend(fontsize = 7.9, bbox_to_anchor=(0.61, 1), loc=6, borderaxespad=0.)
   file_saved = plt.savefig(dirpath+filename, format='png')
   figure = dirpath+filename+".png"
   return render_to_response("myapp/mypage.html", {'file_saved':figure},context_instance = RequestContext(request))
  </code></pre>
  
  <p><strong>I am not sure what to use in urls.py? help please?</strong></p>
  
  <p>In html:</p>
  
  <pre><code>        &lt;body&gt;
              &lt;!-- Uplading files! --&gt;
              &lt;p&gt;&lt;a href="{% url 'list' %}" align="right"&gt;Return to home&lt;/a&gt;&lt;/p&gt;
              &lt;p&gt; Please, starting uploading your files &lt;p&gt;
  
                      &lt;form action="{% url "UploadFile" %}" method="post" enctype="multipart/form-data"&gt;
                      {% csrf_token %}
                      &lt;input type="file" name="files" multiple/&gt;
                      &lt;input type="submit" value="Upload" /&gt;
              &lt;/form&gt;
  
              &lt;form action="{% url "list" %}" method="post" enctype="multipart/form-data"&gt;
                      {% csrf_token %}
  
                      &lt;!-- input type="file" name="files" multiple/--&gt;
                      &lt;input type="submit" value="Graphing" /&gt;
              &lt;/form&gt;
               &lt;br&gt;
               &lt;div style="overflow:hidden;"&gt;
               {% load staticfiles %}
  
  
                      &lt;img src="{% static "myapp/afile1-M1" %}" alt="My image" height="400" width="400" float=left hspace="30" border="1.8" /&gt;
  </code></pre>
  
  <p>When I runserver, it displays the graph into a new window. I want to do a couple of things. One, I want to save the figure into a file. Second, display it in the same browser window without the need to go to another window.</p>
  
  <p>Thanks a lot in advance</p>