How to stylize the EqualTo Validator message in Flask?

I have the validator

EqualTo('pass_confirm', message='Passwords Must Match!')

which when the passwords don’t match shows an error like this.

How do I stylize the error message to be red? I’ve already tried changing the message to a <span style="color:red">, but it renders plain text and not the HTML.


enter image description here


Go to Source
Author: Alex F

REMEMBER_COOKIE_DURATION in flask is not working

I am trying to configure my application to expire cookies after some amount of time however they are not working as expected, my cookies don’t get expired at all.

I have the following configuration in my app.py file:

from flask_login import LoginManager
from datetime import timedelta

app.config['REMEMBER_COOKIE_DURATION'] = timedelta(seconds=30)

login = LoginManager(app)
login.init_app(app)
login.login_view = 'login'

The following is my remember_me input:

<input type="checkbox" class="form-check-input" id="remember_me" name="remember_me">
<label class="form-check-label" for="remember_me">Remember me for 1 week</label>

Finally the following is my view function:

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return flask.redirect(flask.url_for('home'))
    if flask.request.method == 'POST':
        member_username = flask.request.form['member_username']
        member_password = flask.request.form['member_password']
        remember_me = flask.request.form.get('remember_me')

        existing_user = Users.query.filter_by(username=member_username).first()

        if existing_user is None or not existing_user.check_password(member_password):
            flask.flash('Invalid username or password')
            return flask.redirect(flask.url_for('login'))

        if existing_user:
            login_user(existing_user, remember=remember_me)

        next_page = flask.request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = flask.url_for('home')
        return flask.redirect(next_page)
    return flask.render_template('login.html')

The resources I have already followed:
https://code.luasoftware.com/tutorials/flask/how-to-configure-flask-login/
https://www.youtube.com/watch?v=CRvV9nFKoPI
flask-login: Chrome ignoring cookie expiration?

What am I missing?

Go to Source
Author: sherlock

Flask setup using SSL and subdomain on Ubuntu server

I’m am new to Flask and trying to set up a flask webpage on the subdomain https://ohl.bhelpful.net. I have another webpage running on https://bhelpful.net that has nothing to do with this flask app.

When I enter “service apache2 reload” and try to go on https://ohl.bhelpful.net it just gives me this:
Error when trying to view webpage

The main website (just a plain HTML) on https://bhelpful.net looks like this:

Image of main website working

My config file looks like this:

/etc/apache2/sites-available/default-ssl.conf:

Listen 443
<IfModule mod_ssl.c>

        <VirtualHost *:443>

                ServerAdmin webmaster@localhost

                DocumentRoot "/home/bhelpful/html"
                ServerName bhelpful.net
                ServerAlias www.bhelpful.net

                SSLEngine on
                SSLCertificateFile "/etc/ssl/bhelpful_net.crt"
                SSLCertificateKeyFile "/etc/ssl/private/bhelpful_net.key"

        </VirtualHost>

        <VirtualHost *:443>

                ServerAdmin webmaster@localhost
                ServerName ohl.bhelpful.net
                WSGIScriptAlias / /home/ohl/ohl.wsgi
                <Directory /home/ohl/Online-Help-List/app/>
                        Order allow,deny
                        Allow from all
                </Directory>

                Alias /static /home/ohl/Online-Help-List/app/static/
                <Directory /home/ohl/Online-Help-List/app/static/>
                        Order allow,deny
                        Allow from all
                </Directory>

                ErrorLog ${APACHE_LOG_DIR}/FlaskApp-error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/FlaskApp-access.log combined

                SSLEngine on
                SSLCertificateFile "/etc/ssl/bhelpful_net.crt"
                SSLCertificateKeyFile "/etc/ssl/private/bhelpful_net.key"

        </VirtualHost>

</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

The WSGI file looks like this:

/home/ohl/ohl.wsgi

#!/usr/bin/python3.8
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/home/ohl/Online-Help-List")

from app.help_me import app as application

The main flask file lays here:
/home/ohl/Online-Help-List/app/help_me.py

Treeview of files and folders

Go to Source
Author: Andreas G.D Petersen

Flask applicatio HTML Code Not Accessing Images

I have written a basic application in flask (in a docker image) which has an HTML page that needs to show two images. Due to some strange problem, the server is getting 404 errors for both of the images even though the images are at the right path. Attached are the screenshots of the directory output, and the error. Following is the HTML tag calling that image file:

<img src="input1.jpg" alt="" width="130" height="100" />

The directory structure in the container leading to the image:

apptemplatesinput1.jpg

The directory structure in the container leading to the HTML page is:

apptemplatesresult.html

Error line:

17.0.1 - - [29/Aug/2020 13:54:47] "GET /templates/input1.jpg HTTP/1.1" 404 -

Thank you.

Go to Source
Author: Muhammad Usman

redis connection error within docker container

I’m trying to setup a light-weight RQ task queue app running in Docker. I keep on getting this error:

redis.exceptions.ConnectionError: Error -2 connecting to redis:6379. Name or service not known.

or

ValueError: Redis URL must specify one of the following schemes (redis://, rediss://, unix://)
I get the first error if I hardcode redis://redis:6379 and the second when grabbing from environmental variable (os.getenv()). I looked at: link1 and link2, but there wasn’t really a solution, the thread went dead.

My redis connection code is like so:

import os
import redis
from rq import Worker, Queue, Connection

listen = ['default']
redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379')

if __name__ == '__main__':
    print(f"redis url: {redis_url}")
    with Connection(redis.from_url(redis_url)):
        worker = Worker(list(map(Queue, listen)))
        worker.work()

When it prints the redis_url it is correct; I set the environmental var, REDIS_URL to redis://redis:6379 where redis is the hostname in docker-compose.yml.

  redis:
    image: redis:alpine
    expose:
      - '6379'

Appreciate any help. Thanks.

Go to Source
Author: Spencer Trinh