Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to save the edited content? #6

Closed
highwindmx opened this issue May 1, 2018 · 1 comment
Closed

how to save the edited content? #6

highwindmx opened this issue May 1, 2018 · 1 comment

Comments

@highwindmx
Copy link

Great work
Do you have a demo about how to save/upload the edited content

@greyli
Copy link
Member

greyli commented May 2, 2018

It depends on the tool you use. For example, if you use Flask-WTF and Flask-SQLAlchemy, then the save/upload operation will just like any normal form input:

import os

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy
from flask_ckeditor import CKEditor, CKEditorField

app = Flask(__name__)

app.secret_key = 'secret string'

ckeditor = CKEditor(app)
db = SQLAlchemy(app)


class Post(db.Model):  # database model class
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(64))
    body = db.Column(db.Text)


class PostForm(FlaskForm):  # form class
    title = StringField('Title')
    body = CKEditorField('Body', validators=[DataRequired()])
    submit = SubmitField('Submit')


@app.route('/write', methods=['GET', 'POST'])
def write():
    form = PostForm()
    if form.validate_on_submit():  # create new post
        title = form.title.data
	body = form.body.data
        post = Post(title=title, body=body)  # create record instance
        db.session.add(post)  # add to database session
        db.session.commit()  # commit change into database      
	return render_template('post.html', title=title, body=body)
    return render_template('write.html', form=form)


@app.route('/edit/<int:post_id>', methods=['GET', 'POST'])
def edit(post_id):
    post = Post.query.get_or_404(post_id)  # query post in database
    form = PostForm()
    if form.validate_on_submit():  # edit/update created post
        post.title = form.title.data  # set new value
	post.body = form.body.data  # set new value
        db.session.commit()  # commit change into database      
	return render_template('post.html', title=title, body=body)
    form.body.data = post.body  # preset the form input data
    return render_template('edit.html', form=form)

You can check Flask-SQLAlchemy's documentation for more infomation.

@greyli greyli closed this as completed May 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants