-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
239 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FLASK_DEBUG=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
|
||
from flask_ckeditor import CKEditor | ||
from flask import Flask, render_template, flash | ||
from flask_wtf import FlaskForm | ||
from wtforms import StringField, SubmitField | ||
from wtforms.validators import DataRequired, Length | ||
from flask_ckeditor import CKEditorField | ||
from bleach import clean | ||
|
||
app = Flask(__name__) | ||
app.secret_key = os.getenv('SECRET_KEY', 'secret string') | ||
ckeditor = CKEditor(app) | ||
|
||
def clean_html(html): | ||
allowed_tags = ['a', 'abbr', 'b', 'br', 'blockquote', 'code', | ||
'del', 'div', 'em', 'img', 'p', 'pre', 'strong', | ||
'span', 'ul', 'li', 'ol'] | ||
allowed_attributes = ['src', 'title', 'alt', 'href', 'class'] | ||
return clean(html, tags=allowed_tags, attributes=allowed_attributes) | ||
|
||
|
||
class ArticleForm(FlaskForm): | ||
title = StringField('Title', validators=[DataRequired(), Length(1, 50)]) | ||
body = CKEditorField('Body', validators=[DataRequired()]) | ||
submit = SubmitField('Publish') | ||
|
||
|
||
@app.route('/', methods=['GET', 'POST']) | ||
def index(): | ||
form = ArticleForm() | ||
if form.validate_on_submit(): | ||
title = form.title.data | ||
body = clean_html(form.body.data) | ||
flash('Your article is published!') | ||
return render_template('article.html', title=title, body=body) | ||
return render_template('index.html', form=form) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
body { | ||
margin: auto; | ||
width: 750px; | ||
} | ||
|
||
nav ul { | ||
list-style-type: none; | ||
margin: 0; | ||
padding: 0; | ||
overflow: hidden; | ||
background-color: #333; | ||
} | ||
|
||
nav li { | ||
float: left; | ||
} | ||
|
||
nav li a { | ||
display: block; | ||
color: white; | ||
text-align: center; | ||
padding: 14px 16px; | ||
text-decoration: none; | ||
} | ||
|
||
nav li a:hover { | ||
background-color: #111; | ||
} | ||
|
||
main { | ||
padding: 10px 20px; | ||
} | ||
|
||
.alert { | ||
position: relative; | ||
padding: 0.75rem 1.25rem; | ||
margin-bottom: 1rem; | ||
border: 1px solid transparent; | ||
border-radius: 0.25rem; | ||
color: #004085; | ||
background-color: #cce5ff; | ||
border-color: #b8daff; | ||
} | ||
|
||
.error { | ||
color: #721c24; | ||
background-color: #f8d7da; | ||
border-color: #f5c6cb; | ||
} | ||
|
||
.note { | ||
padding: 5px 10px 10px; | ||
border-left: solid 2px #bbb; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.note form { | ||
display: inline; | ||
} | ||
|
||
.info { | ||
color: grey; | ||
} | ||
|
||
.btn { | ||
font-family: Arial; | ||
font-size: 12px; | ||
padding: 5px 8px; | ||
text-decoration: none; | ||
cursor: pointer; | ||
background-color: white; | ||
color: black; | ||
border: 1px solid #999999; | ||
} | ||
|
||
.btn:hover { | ||
text-decoration: none; | ||
border: 1px solid black; | ||
} | ||
|
||
footer { | ||
font-size: 13px; | ||
color: #888; | ||
border-top: 1px solid #eee; | ||
margin-top: 25px; | ||
text-align: center; | ||
padding: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block title %}{{ title }}{% endblock %} | ||
|
||
{% block content %} | ||
<h1>{{ title }}</h1> | ||
<p>{{ body|safe }}</p> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
{% block head %} | ||
<meta charset="utf-8"> | ||
<title>{% block title %}HelloFlask - CKEditor Example{% endblock %}</title> | ||
<link rel="icon" type="image/x-icon" href="{{ url_for('static', filename='favicon.ico') }}"> | ||
{% block styles %} | ||
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css' ) }}"> | ||
{% endblock %} | ||
{% endblock %} | ||
</head> | ||
|
||
<body> | ||
<nav> | ||
<ul> | ||
<li><a href="{{ url_for('index') }}">HelloFlask</a></li> | ||
</ul> | ||
</nav> | ||
<main> | ||
{% for message in get_flashed_messages() %} | ||
<div class="alert">{{ message }}</div> | ||
{% endfor %} | ||
{% block content %}{% endblock %} | ||
</main> | ||
<footer> | ||
{% block footer %} | ||
<small> © 2024 <a href="https://greyli.com">Grey Li</a> / | ||
<a href="https://github.com/greyli/helloflask">GitHub</a> / | ||
<a href="https://helloflask.com">HelloFlask</a> | ||
</small> | ||
{% endblock %} | ||
</footer> | ||
{% block scripts %}{% endblock %} | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{% extends 'base.html' %} | ||
{% from 'macros.html' import form_field %} | ||
|
||
{% block content %} | ||
<h1>New Article</h1> | ||
<form method="post"> | ||
{{ form.csrf_token }} | ||
{{ form_field(form.title) }} | ||
{{ form_field(form.body) }} | ||
{{ form.submit }} | ||
</form> | ||
{% endblock %} | ||
|
||
{% block scripts %} | ||
{{ super() }} | ||
{{ ckeditor.load() }} | ||
{{ ckeditor.config(name='body') }} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% macro form_field(field) %} | ||
{{ field.label }}<br> | ||
{{ field(**kwargs) }}<br> | ||
{% if field.errors -%} | ||
{% for error in field.errors -%} | ||
<small class="error">{{ error }}</small><br> | ||
{%- endfor %} | ||
{%- endif %} | ||
{% endmacro %} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters