Flask - action when HTML button is pressed? - html

I am trying to make dynamically generated download pages for some uploaded files on my site.
I've got routing set up and a template that will show the filename. I want to have a button on this page, that when pressed will call send_from_directory and download the file. How can I do this?
My function in python that renders the download page:
#app.route('/<new_folder_name>/', subdomain='f')
def uploaded_file(new_folder_name):
filename = subfolder_fetch(new_folder_name)
return render_template("download.html", filename=filename)
My button in html is just something like:
{% block body %}
<div id = "filename">
{{filename}}
</div>
<button name="dlbutton">Download</button>
{% endblock %}
I don't have a form or anything set up, do I need to do something like set one up and then catch the request with flask? How would I do this, or is there a simpler way?

Create a download_file route with your send_from_directory response and use a link to download your file:
Download

Related

Httml Button in Django app to run a Python script

I am using Geonode that is actually a django app installed inside a docker container. What I want to do is, to edit an html page of my app and add an extra button that when the user presses it a python script will run.
So far, I have added the button in the html page, a function in my views.py file and a url in the urls.py but it doesnt seem to work.
html page
{% trans "Approve Layer" %}
views.py
def layer_approval(request):
# I keep it simple to make sure it works
return redirect('www.google.gr')
urls.py
from django.urls import path
urlpatterns += [
url(r'^layer_approval/', include('geonode.views.layer_approval'))
]
I am completely new to Django. Any advice?
Your method name and URL-pattern layer_approval and pass button name or id layer_approve.

Show the download button only if file is available on server in Django template (html)

I am using the following template code to show the Download option on the webpage.
<a href="{%static 'media/finance'%}/sap-daily.csv" download >Download</a>
But I want to only show the Download option if the file is present on the server and otherwise show not available option.
I am using python 2.7 and django 1.6.7 .
What you can do is check if the file exists in your views.py with
from pathlib import Path
my_file = Path("/path/to/file")
if my_file.is_file():
show_link = True
Then in your template HTML:
{% if show_link %}
<a href="{%static 'media/finance'%}/sap-daily.csv" download >Download</a>
{% endif %}

How do I make <a href> link to open in a new tab but in an iFrame part of another template?

I have created a Django web app which throws document recommendation to users based on their search query; the next thing I am trying to achieve is to able to make users open these recommended documents in a new tab but in an iFrame which would part of another template.
Also, I just want to use HTML and Python to achieve this result.
Till now, I am just able to open the documents in a new tab which is not part of my Django framework
<html>
<body>
{% for document in Documents %}
<a href = "{link to the corresponding document on my system}"
target = "_blank">
<p><font color = "blue"><b>{{ document }}</b></font></p>
</a>
{% endfor %}
</body>
</html>
The above code loops through the list of recommended documents generated by model and prints their hyperlinked names on an HTML page. These links just open the document in a new tab but I want them to open in an iFrame on a new template which is part of my Django framework.

Import a static HTML website to Django CMS

I have designed and coded a website (with bootstrap 4) and now I would like to import it in Django CMS so the client can edit the content.
Any tips or guide that helps me to achieve this?
Thanks a lot
You have to make it into a template and import the information itself separately. Add new template to settings.py.
If your client already has a basic template that you're editing you may simply edit his existing files and CSS. Alternatively, you may also download/import an existing template and use it as your base for editing.
The template itself will look something like this: default template: base.html,
{% load i18n %}
{% extends 'base.html' %}
{% block title %}Title Here{% endblock %}
{% block content %}
{% endblock %}
#A-creative At first you should copy Bootstrap 4 static files (css and js) into your "projectname/appname/static" dir, run "python manage.py collectstatic" and then just copy your Bootstrap-ready html into the cms using Style, Text and Snippet plugin fields (Admin > Create new page > Edit > Add plugin/block > ...). You should do it page by page.
I doubt there is a shorter way... Yeap, and your should use a standard minimalistic template as mentioned by #Patriot to avoid dealing with template issues and plugin / html issues at the same time.

External HTML file in Django

I'm making Django app in which I need to embed many external HTML files in the template. Each HTML file is stored in its own directory, along with the subdirectory that contains all the images. The file structure:
Abstract1
Pictures
image1.png
image2.png
abstract1.html
I use a custom template tag for embedding (see below). My problem: the HTML files are loaded, but linked resources (e.g. img) are not working properly (i.e. they're not being displayed). HTML files use relative urls, which, mixed with the django template base path produce invalid url, but even if I use hardcoded absolute urls the problem remains. I feel like I'm missing something obvious. Is there some proper (or not proper but working) way to overcome such problem?
template
{% load abstracts_extras %}
<!DOCTYPE html>
<html>
<body style="margin-left:10px">
<h2>{{abstract}}</h2>
<b>Authors:</b><br/>
<ul>
{% for author in authors %}
<li>{{author}}</li>
{% endfor %}
</ul>
<p>
<b>Title: </b>{{abstract.title}}
<p>
<hr>
{% include_external filename|add:'.html' %}
</body>
</html>
abstracts_extras
from django.template import Library
register = Library()
def include_external (url):
url = 'file:///' + url
import urllib2
return urllib2.urlopen (url).read ()
If I am understanding well, your templates load but not statics like img.
It would be a configuration error.
You should check both settings.py for Django and httpd.conf for Apache and check staticfiles are properly configured.
Have you any error shown or just images are not loaded (but no error)?