Update content of static html file in Django - python

I saved an HTML as a static file in order to save it in cache and render it through a service worker when Offline. I would like to make a small change on my HTML in order to get the appropriate encoding. I am using pythonanywhere as my server. And I have defined in my settings.py the following paths:
STATIC_URL = '/static/'
STATIC_ROOT = u'/home/cocoa/cocoa/static'
When I look at my file in the server in the following directory is:
/home/cocoa/cocoa/static/login/Offline/HTML/mEstado.html
My file looks like this:
If I visit the following URL:
https://cocoa.pythonanywhere.com/static/login/Offline/HTML/mEstado.html
My file looks like this:
I allready tried reloading on shift. Unabling the service-worker and
python manage.py collectstatic
But nothing seems to work. I do not know where the problem is, nor how to solve it. Any help would be appreciated.

Did you try to clear you browser cache? Ctrl+F5. I think problem in cached html file because you run it from static. As I know browser caches all static files from django static folder.

Related

django static file not loading after updating it on production

On a django web app. I have a script which runs when i goes to myapp.com/update url it fetches data using bs4 library and convert the data into a pandas dataframe.
To show that data I used pd.to_html to convert it in HTML table format and stores that HTML file in static folder and then load it in my index.html(which present in templates folder using jquery).
The HTML loads fine but after the I visit the update url it stops showing the updated HTML file (pandas df one). The updated HTML file (pandas df one) is still available on azure panel. The site is not loading it anymore but before update it was loading perfectly.
How do I display the updated HTML file?
--script in my views.py (for myapp.com/update url)--
text_file2 = open("staticfiles/album.html", "w")
text_file2.write(updatedData)
text_file2.close()
This is how I'm adding that HTML file to my main index.html file
(this loads the file perfectly but after update url it stops loading it)
<script>
$(function () {
$("#includedAlbums").load("../static/album.html");
});
</script>

How to download auto generated file in Django?

I am created a basic ocr system. When user uploads a PDF file to system ocr read it and created a xlsm file with the same name. I am using remote control with Azure (Windows). I created a download button. When I click it, downloads the file with same name, so it should work but appears a error.
How can I fix this problem?
<a href= "{% static path %}" class="btn btn-outline-primary btn-sm" download>Download Report File</a>
in my views:
name = pdf.pdf.name.replace(".xlsm", "").replace(".pdf", "")
path = "C:/user/ocr/" + name + ".xlsm"
Note: Uploaded and created files are stored outside of the project file.
please get more error info from django server. and list more code from my views.
Do you want to change file filename?

Best practice to config the dynamically generated file

I made the file like this in views.py.
csvDir = 'exportedCsv/'
file_path = csvDir + 'test.csv'
df.to_csv(path_or_buf=file_path,sep=',',float_format='%.2f',index=False,decimal=",",encoding='utf_8_sig')
Dynamically generate file and path the filepath to html.
Now there is my file in /exportedCsv/test.csv
However I have no way to access this from html.
My idea is basiclly wrong???
What should I do when I want to make csv file and let the user download it??
You could add a link to it in your html like so: Download

No such file or directory attach_file in django email

attach_file is not picking the absolute url although file exist. its able to pic internal url and send file but not the absolute url
email.attach_file("http://devuserapi.doctorinsta.com/static/pdfs/Imran_1066.pdf",mimetype="application/pdf")
this file opens when i copy paste the url in browser. what could be the issue.
Thanks in advance
attach_file takes a file from your filesystem, not a URL, so you have to use a local path to it
See https://docs.djangoproject.com/en/1.9/topics/email/
One, untested, possibility is to use the attach method instead and to download the file on the fly:
import urllib2
response = urllib2.urlopen("http://devuserapi.doctorinsta.com/static/pdfs/Imran_1066.pdf")
email.attach('IMran_1066.pdf',response.read(),mimetype="application/pdf")
It lacks error checking to make sure the file was downloaded, of course, and I haven't actually tried it myself, but that might be an alternative for you.

How do I allow users to download a MIDI file with Flask without getting a 0 byte download?

I am writing an application that creates a midi file using the MIDIUtil library. When the user submits an HTML form, a midi file object is created with MIDIUtil. How do I allow the user to download this as a .mid file? I have tried the following code, but I end up downloading a file of 0 bytes.
return Response(myMIDIFile, mimetype='audio/midi')
I use a variant of the following code to allow my users to download images they generate. The below code should work for you. Please note that you will most likely need to specify the full server path to the file being downloaded.
from flask import send_file
download_filename = FULL_PATH_TO_YOUR_MIDI_FILE
return(send_file(filename_or_fp = download_filename,mimetype="audio/midi",as_attachment=True))
I ended up using this, and it worked.
new_file = open('test.mid', 'wb')
myMIDI.writeFile(new_file)
new_file.close()
new_file = open('test.mid', 'rb')
return send_file(new_file, mimetype='audio/midi')
Might want to just try using send_file
from flask import send_file
return send_file("yourmidifile.mid", as_attachement=True, mimetype="audio\midi")

Categories

Resources