I have referred to multiple django question regarding file download and this was the solution suggested.
{% for task in tasks %}
<tr><td><strong> name {{task.name}}</strong></td>
<td><strong> date {{task.date_created}}</strong></td>
<td><strong> status {{task.status}}</strong></td>
<td><strong> id {{task.id}}</strong></td>
<td><strong> input file {{task.input_file_path}}<td>
<td><a href="{{task.output_file_path}}" download>output file</td></tr>
{% endfor %}
The solution suggested is <a href="{{task.output_file_path}}" download> However, when i check my django request. It shows up this in console. GET /adv_filters/check/download/download/download/download/download/download/download/download/download/download/download/download/download/download/download/download/download/download/download/download/input_files/input_files/input_files/input_files/input_files/task4 HTTP/1.1" 200 2524
For testing purposes. I have set the location of the output file to be the location of the input file.
My relevant views.py
document.input_file_path = 'input_files/' + document.name
document.output_file_path = 'input_files/' + document.name
Models.py
doc = models.FileField(upload_to='input_files')
Is there anymore information i would need.
I guess simply putting file link(i.e. which I am assuming {{task.output_file_path}} is) should work.
<td><a href="{{task.output_file_path}}"</a> Download output file</td>
Related
My webpage has a functionality that lets users request fairly large json files (3MB+) to be generated. The generation process takes some time and I send them an email with a download link once it is ready.
My problem is that clicking the download URL will open the content of the json file in the browser instead of starting a download dialog.
I have found this question: Serving .json file to download but the solution reserialize the json file into the response:
mixed_query = list(invoices) + list(pcustomers)
json_str = serializers.serialize('json', mixed_query))
response = HttpResponse(json_str, content_type='application/json')
response['Content-Disposition'] = 'attachment; filename=export.json'
As the JSON file already exists, I do not want to rewrite it entirely within the response. I just want users to download the already generated file.
How can I force the URL to start a download when reached?
I am using Django 3.1, with Python 3.6
Here is the email template sent to users once the download link is ready:
{% extends "emails/email_css.html" %}
{% block mail_content %}
<p>Hi, {{ username }}</p>
<p>Your export for {{ export_name }} is ready!</p>
<p>The file will be available for download for 2 days, or until you request a new export.</p>
<a class="download_button" type="button" href={{ download_url }}>Download</a>
{% endblock %}
Edit:
I also tried this solution How to download file using anchor tag <a> but the json file still gets shown in the browser instead of being downloaded.
I eventually found how to do it, by returning a FileResponse with as_attachment=True.
from django.http import FileResponse
def download_export(request):
response = FileResponse(open("path/to/file.json", 'rb'), as_attachment=True,
filename="file_name.json")
return response
Try adding the download property to the <a> tag. You can optionally specify a download filename or not.
Without filename:
<a class="download_button" type="button" href={{ download_url }} download>Download</a>
With filename:
<a class="download_button" type="button" href={{ download_url }} download="someJsonFile.json">Download</a>
See docs here for the download property. Also the related StackOverflow post here.
I am working on an informational site. I am attempting to access files in a subdirectory using data from the django db.
Ex: if the record.id = 1 and the file is desc.txt, I want to grab /1/desc.txt
I currently have two items that I'm trying to access this way. The image file will work if I hard code the location, but I haven't been able to get it to work from the record data. The other one, I'm trying to load a description file into a div using jQuery and .html(), and can't get it to work at all.
The relevant section of code:
{% if aRecord %}
{% load static from staticfiles %}
<h1>{{ aRecord.Name }}</h1>
<br/>
Born: {{aRecord.Birth}}<br/>
Died: {{aRecord.Death}}<br/>
<div id="descArea"><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/></div>
<script>
descfile = 'Authors/1/desc.txt';
$('#descArea').text(descfile);
$.get("{% static 'Authors/1/desc.txt' %}", function(data) {
$('#descArea').html(data);
});
</script>
<br/>
{% else %}
Requested author not found.
{% endif %}
This is the hard-coded version that works, but I need it to work from the aRecord data. The 1 in both cases should come from the record id, and the image filename in the img tag should as well.
It sounds like this is really a question about how to produce a file path from dynamic data. Is that the case? If so, you will probably need to use string substitution.
In order to produce a string like '/1/desc.txt' where the 1 is given by a record id, you could do something like this:
path = '/{}/desc.txt'.format(record.id).
I'm attempting to add a link to an icon I used to the footer of my doc page, but I can't seem to figure out how to copy this link. I'm following this tutorial but haven't had any luck. I've created a file _templates/layout.html:
{% extends "!layout.html" %}
{% block footer %}
<li>CC BY 3.0</li>
{{ super() }}
{% endblock %}
then in my conf.py I do
templates_path = ['_templates']
# ...
html_theme = 'sphinx_rtd_theme'
The problem is that when I build, nothing extra shows up in the footer of my page. I initially thought sphinx just wasn't finding my file, but if I change {% extends "!layout.html" %} to {% extends "layout.html" %} I get the error
Running Sphinx v1.3.1
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 30 source files that are out of date
updating environment: 0 added, 0 changed, 0 removed
looking for now-outdated files... none found
preparing documents... done
writing output... [ 3%] dev/conventions
Exception occurred:
File "C:\...\Anaconda\lib\site-packages\jinja2\utils.py", line 389,
in __getitem__
if self._queue[-1] != key:
RuntimeError: maximum recursion depth exceeded in cmp
The full traceback has been saved in c:\...\appdata\local\temp\1\sphi
nx-err-tjhk_m.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message c
an be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphin
x/issues>. Thanks!
So I know sphinx see's my file, but it doesn't seem to write anything. What am I doing wrong?
The issue I ended up having was I was overwriting the wrong file, while layout.html did implement a footer block, it was not the block I was looking to add to. Instead sphinx_rtd_theme has a footer.html file, which I ended up overwriting instead and everything worked as intended.
I am doing internationalisation in django admin.I am able to convert all my text to the specific langauge.But i m not able to change the 'app' name
suppose
django-admin.py startapp test
this will create a app called test inside my project.Inside this app 'test' i can create many classes in my model.py file.But when i register my app 'test' in settings.py file.I am convert all the text in the locale of my browser but my app heading 'test' is not getting changed.How to change that any idea?
this is a well known problem in django and there is a ticket since 2006
a workaround for this would be to place all the appnames (with upper und lower-case) manually in your *.po-file.
to ensure django-admin will catch this, Replaced this:
<caption>
<a href="{{ app.app_url }}" class="section">
{% blocktrans with app.name as name %}{{ name }}{% endblocktrans %}
</a>
</caption>
with this:
<caption>
{% trans app.name %}
</caption>
maybe this snippet would help too?
Defining an i18n-ized name, or any other custom app name is not natively possible.
Check out:
Can you give a Django app a verbose name for use throughout the admin?
http://code.djangoproject.com/ticket/3591
As mentioned in the question and ticket, there are several workarounds.
I am trying to install django-photologue. Everything seems ok, because I install and set up following the official guidelines. I have to upload some photos as examples. However, when viewing a photo or gallery details , then an error as follows:
Caught an exception while rendering: 'Photo' object has no attribute 'get_thumbnail_url'
I tried to remove the following code from the file photo_detail.html
{% if object.public_galleries %}
<h2>This photo is found in the following galleries:</h2>
<ol>
{% for gallery in object.public_galleries %}
<li>{%previous_in_gallery object gallery%} {{ gallery.title }} {%next_in_gallery object gallery%}</li>
{% endfor %}
</ol>
{% endif %}
No more errors, but pictures do not show up. If you click on the link will still lead to correct photographs to see. I think the problem in:
{{ object.get_display_url }}
It is totally not return any value.
Please help me solve this problem. Thanks!
Did you run python manage.py plinit after install and opt to create both a thumbnail and display photosize? These photosizes need to be defined in your database.
In other versions, you have to edit photologue/templates/photolog/tags/next_in_gallery.html and replace
{{ photo.get_thumbnail_url }}
with
{{ photo.thumbnail.url }}
Same for photologue/templates/photolog/tags/prev_in_gallery.html.
Honestly from looking at the source, it looks like a bug in the project. If you search the source, thumbnail doesn't seem to be a field within the Photo class (get_FIELD_url is an easy way to access an ImageField's url btw.) So I would recommend tinkering with the source or finding another project. I might be wrong though but that's what my ~5 minute scan of the project found.