Created
September 24, 2021 21:26
-
-
Save dominiksimgen/29d0cd5341f52d78ab1a2fb10fc9903b to your computer and use it in GitHub Desktop.
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
# allows python code to be embedded in html | |
#enables basic arithmetics | |
<body> | |
{{3*3}} | |
</body> | |
#pass over variables from the python backend to your HTML | |
# server.py | |
@app.route('/') | |
def home(): | |
random_number = random.randint(1,10) | |
return render_template('index.html', num=random_number) | |
# index.html | |
<body> | |
{{ num }} | |
</body> | |
# multiline code | |
#for loop | |
<body> | |
<div> | |
{% for blog_post in blog_posts: %} | |
<h1>{{ blog_post['title'] }}</h1> | |
<h2>{{ blog_post['subtitle'] }}</h2> | |
<p>{{ blog_post['body'] }}</p> | |
<br> | |
{% endfor %} | |
</div> | |
</body> | |
# if else statement | |
<div> | |
{% if (1+1) == 2%} | |
<h1>Equation is true!</h1> | |
{% else %} | |
<h1> Equation is false!</h1> | |
{% endif %} | |
</div> | |
# url building | |
<a href=" {{ url_for('get_blog') }}">Go to blog</a> # get_blog calls the get_blog() function within the flask server app | |
# this can also be used to pass over keyword parameters | |
<a href=" {{ url_for('get_blog', num=3) }}">Go to blog</a> | |
# static files could be linked in this was as well | |
<link href="{{ url_for('static', filename='css/styles.css')}}" rel="stylesheet" /> | |
# formating numbers and typecasting | |
{{ number|int }} | |
{{ "$%.2f"|format(value|float) }} | |
# templating | |
{% include "header.html" %} # inserts html code from another html file | |
<body> | |
... | |
</body> | |
{% include "footer.html" %} | |
# template inheritance | |
# base.html (this is the template) | |
... | |
<head> | |
<meta charset="UTF-8"> | |
<title>{% block title %}{% endblock %}</title> | |
</head> | |
<body> | |
{% block content %}{% endblock %} | |
</body> | |
... | |
# index.html (inherits from the template ) | |
{% extends "base.html" %} | |
{% block title %} Title lives here {% endblock %} | |
{% block content %} | |
...content lives here... | |
{% endblock %} | |
# inheritance and super block | |
# base.html | |
<style> | |
{% block styling %} | |
body{ | |
background: purple; | |
} | |
{% endblock %} | |
</style> | |
# index.html inherits the styling from base.html and adds custom h1 styling | |
{% block styling %} | |
{{ super() }} | |
h1 { | |
color:red; | |
} | |
{% endblock %} | |
# add custom style via css stylesheet (for example to ovverride Bootstrap standard style) | |
{% block styles %} | |
{{ super() }} | |
<link rel="stylesheet" href="{{url_for('static',filename='css/styles.css')}}"> | |
{% endblock %} | |
# message flashing | |
from flask import Flask, render_template, request, url_for, redirect, flash | |
@app.route('/login', methods=["GET", "POST"]) | |
def login(): | |
if request.method == "POST": | |
email = request.form.get('email') | |
password = request.form.get('password') | |
user = User.query.filter_by(email=email).first() | |
#Email doesn't exist | |
if user == None: | |
flash("That email does not exist, please try again.") | |
return redirect(url_for('login')) | |
#Password incorrect | |
elif check_password_hash(user.password, password) == False: | |
flash('Password incorrect, please try again.') | |
return redirect(url_for('login')) | |
#Email exists and password correct | |
else: | |
login_user(user) | |
return redirect(url_for('secrets')) | |
return render_template("login.html") | |
#"login.html" | |
{% with messages = get_flashed_messages() %} | |
{% if messages %} | |
{% for message in messages %} | |
<p>{{ message }}</p> | |
{% endfor %} | |
{% endif %} | |
{% endwith %} | |
# change website rendering based on login status | |
{% if current_user.is_authenticated %} | |
Hi {{ current_user.name }}! | |
{% endif %} | |
{% if not current_user.is_authenticated %} | |
Nobody is logged in! | |
{% endif %} | |
{% if current_user.id == 1 %} | |
<a href="{{url_for('delete_post', post_id=post.id) }}">✘</a> | |
{% endif %} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment