I'm working on a website/blog setup for myself using Flask, SQLite (via SQL Alchemy), WTForms, and Bootstrap. I have a form to add a blog post ("contentmaker"), and a form to add a file upload (retitling the file, creating a file reference including stuff like a description in my database, etc) which I am opening in a popup window.
Right now, it just uploads the file and reloads itself. What I want to do is close the upload popup window and pass the path the file was saved to and the file.id from the newly created database entry back to the blog post -- the first to be used in the post content (at this time, I'm focused on adding images) and the second to create a cross-reference in the database, once the post is actually created, so I'll be able to track which posts reference which file.
For some reason this simple step is breaking my brain -- probably because it uses javascript, and I've never been able to wrap my head around javascript. I cannot seem to grasp what steps I need to take to pass the data back.
Here is my routes.py setup for the upload:
@main.route('/backend/upload', methods=['GET','POST'])
def upload():
form = UploadForm()
#process the uploaded file, substituting user filename if provided
if form.is_submitted():
uploaded = form.upload.data
#Clean up submitted title info so it can also be used as a filename.
fname = form.fname.data.translate(str.maketrans('', '', string.punctuation))
fname = fname.replace(" ", "-")
ext = uploaded.filename.rsplit('.',1)
ext = '.' + ext[1]
fname = fname + ext
#Upload the file to different locations depending on the file types.
if ext == '.zip':
uploaded.save(os.path.join(current_app.config['UPLOAD_PATH_ZIP'], fname))
type = 'archive'
elif ext == '.pdf' or ext == '.txt' or ext == '.rtf':
uploaded.save(os.path.join(current_app.config['UPLOAD_PATH_DOC'], fname))
type = 'doc'
elif ext == '.jpg' or ext == '.jpeg' or ext == '.png' or ext == '.gif':
uploaded.save(os.path.join(current_app.config['UPLOAD_PATH_IMG'], fname))
type = 'img'
else:
abort(400)
# Now add the file to the database.
filedata = File(filename = fname,
filetype = type,
filetitle = form.fname.data,
filedesc = form.fdesc.data)
db.session.add(filedata)
db.session.commit()
return render_template('upload.html', title = 'upload a file', form=form)
return render_template('upload.html', title = 'upload a file', form=form)
This is where I've gotten on the html templates:
contentmaker.html
{% extends "base.html" %}
{% block scripts %}
{{ super() }}
{{ pagedown.include_pagedown() }}
{% endblock %}
{%block page_content %}
{% endblock %}
upload.html
{%block page_content %}
File Upload
It doesn't really affect the question, but for completeness's sake, the relevant part of my model.py:
#central class -- all blog posts, dictionary entries, snippets, etc.
class Content(db.Model):
__tablename__ = 'content'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Text)
body = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
kind_id = db.Column(db.Integer, db.ForeignKey('kinds.id'))
category_id=db.Column(db.Integer, db.ForeignKey('categories.id'))
comments = db.relationship('Comment', backref='content', lazy='dynamic')
#Images, zips, etc.Includes a short description that can be used for alt text, etc.
class File(db.Model):
__tablename__ = 'files'
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(64), index=True)
filedate = db.Column(db.DateTime, index=True, default=datetime.utcnow)
filetype = db.Column(db.String(64))
filetitle = db.Column(db.Text)
filedesc = db.Column(db.Text)
#Track which content references which files, for future reference if a file is deleted/updated/lost.
contentfile = db.Table('contentfiles',
db.Column('content_id', db.Integer, db.ForeignKey('content.id')),
db.Column('file_id', db.Integer, db.ForeignKey('files.id'))
)
I am certain this is a simple thing to do, but I've been stuck reading tutorials and not understanding how to fit them to my use case for days. I would greatly appreciate someone unsticking me.