Skip to content

Commit

Permalink
Initializing flash messages and adding url_for() in templates (#9)
Browse files Browse the repository at this point in the history
* adding url_for to prevent errors when changing routes

* Update layout.html

* Update app.py
  • Loading branch information
patrzykat authored May 3, 2021
1 parent 02c7b29 commit 48942f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ def FUN_upload_image():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
flash('No file part', category='danger')
return(redirect(url_for("FUN_private")))
file = request.files['file']
# if user does not select file, browser also submit a empty part without filename
if file.filename == '':
flash('No selected file')
flash('No selected file', category='danger')
return(redirect(url_for("FUN_private")))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
Expand Down
20 changes: 15 additions & 5 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<li><a href="/public/">Public</a></li>
<li><a href="{{ url_for('FUN_root') }}">Home</a></li>
<li><a href="{{ url_for('FUN_public') }}">Public</a></li>
{% if session.get("current_user", None) != None %}
<li><a href="/private/">Private</a></li>
<li><a href="{{ url_for('FUN_private') }}">Private</a></li>
{% endif%}
{% if session.get("current_user", None) == "ADMIN" %}
<li><a href="/admin">Admin Dashboard</a></li>
<li><a href="{{ url_for('FUN_admin') }}">Admin Dashboard</a></li>
{% endif %}
</ul>

Expand All @@ -43,7 +43,7 @@
{% else %}
<li>
<a><b>{{ session.get("current_user") }}</b></a></li>
<li><a href="/logout"><b><u>Logout</u></b></a>
<li><a href="{{ url_for('FUN_logout') }}"><b><u>Logout</u></b></a>
</li>
{% endif %}
</ul>
Expand All @@ -52,6 +52,16 @@
</div>
</nav>

{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith %}


<div class="container">

Expand Down

0 comments on commit 48942f9

Please sign in to comment.