Skip to content

Commit 21cf90e

Browse files
authored
Add files via upload
1 parent 6401528 commit 21cf90e

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

app.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from flask import Flask, render_template, request, url_for, flash, redirect
2+
3+
4+
app = Flask(__name__)
5+
6+
app.config['SECRET_KEY'] = 'your secret key'
7+
8+
9+
messages = [{'title': 'Message One',
10+
'content': 'Message One Content'},
11+
{'title': 'Message Two',
12+
'content': 'Message Two Content'}
13+
]
14+
15+
16+
@app.route('/create/', methods=('GET', 'POST'))
17+
def create():
18+
if request.method == 'POST':
19+
title = request.form['title']
20+
content = request.form['content']
21+
22+
if not title:
23+
flash('Title is required!')
24+
elif not content:
25+
flash('Content is required!')
26+
else:
27+
messages.append({'title': title, 'content': content})
28+
return redirect(url_for('index'))
29+
30+
return render_template('create.html')
31+
32+

base.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>{% block title %} {% endblock %} - FlaskApp</title>
6+
<style>
7+
.message {
8+
padding: 10px;
9+
margin: 5px;
10+
background-color: #f3f3f3
11+
}
12+
nav a {
13+
color: #d64161;
14+
font-size: 3em;
15+
margin-left: 50px;
16+
text-decoration: none;
17+
}
18+
19+
.alert {
20+
padding: 20px;
21+
margin: 5px;
22+
color: #970020;
23+
background-color: #ffd5de;
24+
}
25+
26+
</style>
27+
</head>
28+
<body>
29+
<nav>
30+
<a href="{{ url_for('index') }}">FlaskApp</a>
31+
<a href="{{ url_for('create') }}">Create</a>
32+
<a href="#">About</a>
33+
</nav>
34+
<hr>
35+
<div class="content">
36+
{% for message in get_flashed_messages() %}
37+
<div class="alert">{{ message }}</div>
38+
{% endfor %}
39+
{% block content %} {% endblock %}
40+
</div>
41+
</body>
42+
</html>

create.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
<h1>{% block title %} Add a New Message {% endblock %}</h1>
5+
<form method="post">
6+
<label for="title">Title</label>
7+
<br>
8+
<input type="text" name="title"
9+
placeholder="Message title"
10+
value="{{ request.form['title'] }}"></input>
11+
<br>
12+
13+
<label for="content">Message Content</label>
14+
<br>
15+
<textarea name="content"
16+
placeholder="Message content"
17+
rows="15"
18+
cols="60"
19+
>{{ request.form['content'] }}</textarea>
20+
<br>
21+
<button type="submit">Submit</button>
22+
</form>
23+
{% endblock %}

index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
<h1>{% block title %} Messages {% endblock %}</h1>
5+
{% for message in messages %}
6+
<div class='message'>
7+
<h3>{{ message['title'] }}</h3>
8+
<p>{{ message['content'] }}</p>
9+
</div>
10+
{% endfor %}
11+
{% endblock %}

0 commit comments

Comments
 (0)