Recent
- Migrating from Bootstrap 4 to Bootstrap 5
- Using Python's pyOpenSSL to verify SSL certificates downloaded from a host
- Why your website canonical name must be 'www' (or 'app' or something else)
- Flask's SERVER_NAME, subdomains and 404 errors
- Flask, Celery, Redis and Docker
- Celery, Redis and the (in)famous email task example
Most viewed
- Using UUIDs instead of Integer Autoincrement Primary Keys with SQLAlchemy and MariaDb
- Flask with multiple forms on a page posted using AJAX and returning a rendered form result
- SLQAlchemy dynamic query building and filtering including soft deletes
- SQLAlchemy server-side datetime calculations
- Two Flask apps, frontend and admin, on one domain using DispatcherMiddleware
- OWASP Zed Attack Proxy (ZAP) security testing
Tags
AJAXAlembicAlpineApplication settingsBabelBootstrapCachingCaptchaCeleryCryptographyCSRF protectionDeepLDispatcherMiddleWareDNSDockerDocker-composeEmailExceptionsFlaskGunicornIconsIMAPInternetISPConfigJavascriptJinja2MariaDBMultilanguagePentestingPostfixProfilingpyOpenSSLPytestRedisSecurity testingSQLAlchemyTestingTimezoneUbuntu TouchUUIDWerkzeugWheelsWTFormsFlask, WTForms and AJAX: CSRF protection, before_request and multilanguage
29 February 2020 0
Peter
I never really checked if CSRF protection was working in my Flask application, this website. Is it enabled by default? From the Flask_WTF extension documentation:
Any view using FlaskForm to process the request is already getting CSRF protection.
And from the text of Miguel Grinberg's post 'Cookie Security for Flask Applications':
If you are handling your web forms with the Flask-WTF extension, ...
Flask with multiple forms on a page posted using AJAX and returning a rendered form result
22 February 2020 0
Peter
While working on comments system for the first time I bounced into the problem of having multiple WTForms forms on a single page. On every page there also is the search form but this is not a POST form. It is doing a GET. The comments system uses two forms:
Comment form
Comment reply form
The comment form is immediately below the content item, blog post or page, the comment reply form is initially ...
A textarea with a character counter widget for Flask, WTForms and Bootstrap
15 February 2020 1
Peter
I hoped to tell you today that you could comment on the blog posts of this website now. That would have meant that I completed the first implementation of the comments system. Unfortunately I stumbled upon some problems, yes of course, I am a programmer, and one of them involved the TextAreaField.
I just wanted a simple extended version of the WTForm TextAreaField, just add a character counter field ...
Threaded comments using Common Table Expressions (CTE) for a MySQL Flask blog or CMS
8 February 2020 68
Peter
Now that I have blog posts, pages and a contact form, I decided to implement the comments for the blog posts and pages. Not just flat comments but threaded comments, also called nested comments. Some months ago I read about this and I really liked Miguel Grinberg's article: Implementing User Comments with SQLAlchemy.
As often Miguel starts with defining the problem and some hard core theory and explaining ...
The mysterious Flask Application Context, my questions and answers
30 January 2020 0
Peter
When you start with Flask you read a bit about the Application Context, TL;DR. Do not know about you but I certainly did not understand it fully. What is app, what is current_app, what is the difference, you just start programming your application. All the time in the background there is this weird buzz: what exactly is the Application Context ... Then at a certain moment when using a class you instantiated ...
WTForms image picker widget for Flask with Bootstrap 4 without extra Javascript and CSS
24 January 2020 0
Peter
When you sign up for this website you are assigned an avatar image. Of course you can change the avatar in 'your account' and this is done using an image picker. Many image pickers examples can be found on the internet. But this is a Flask site including WTForms and I want the image picker to be generated by the wonderful Jinja macro I am using, see also link below, ok, I modified it a bit. With this ...
OWASP Zed Attack Proxy (ZAP) security testing
13 January 2020 0
Peter
Time for joy or crisis: running the OWASP Zed Attack Proxy (ZAP) application to check vulnerabilities of our web application. Developers often know there are weak points in their code, but there always is a date of delivery. So why not use a tool that list the well-known and important ones? This is my first time using ZAP so if are an expert you best stop reading here. In the links below there ...
Flask, Babel and Javascript language files
6 January 2020 0
Peter
This Flask website is multilanguage. The implementation is described in previous posts. So far all my translations were in the Python code and the HTML templates. On a few places I needed some translations in Javascript and did this by pulling this Javascript code inline in the HTML template. For example, for forms I needed:
e.target.setCustomValidity('Please fill out this field.');
I ...
Flask site penetration tests: security headers and the session cookie
27 December 2019 0
Peter
You created a Flask web application, it is running fine and using https. But is it secure enough? Did you do everything to protect your visitors, did you do everything to prevent malicious attacks?
A good way to proceed on this is to pentest your site. Penetration testing tools, or pen testing tools, can identifying security weaknesses. They identify vulnerabilities in the web application that can ...
Docker on Debian / Ubuntu not respecting ufw firewall settings exposing ports
9 December 2019 1
Peter
Again another unexpected Docker issue. In a previous post I described why and how you must force Docker to use a subnet, to prevent sudden unexpected changes in the network with consequences like mail no longer working.
This post is about Docker not respecting firewall settings, at least when running Debian / Ubuntu and ufw (Uncomplicated Firewall). Docker does not tell you this, and exposes ports, ...
Docker containers suddenly using 192.168.0.0/16 instead of 172.17.0.0/16: services lost
27 November 2019 0
Peter
I have an ISPConfig server with Docker applications. They use the host Postfix mail transfer agent (MTA) to deliver mail to the outside world. Before using the send mail function I have a check if Postfix can be accessed. This works fine. But suddenly mail was not sent. The log file contained error messages like:
2019-11-26 17:31:56,758 ERROR MailMessage - send_mail: self.error_message = sending message, ...
Using Python kwargs (keyword arguments) in Flask url_for() for pagination
24 November 2019 0
Peter
For this website I am using Flask and SQLAlchemy without the Flask-SQLAlchemy extension. I need pagination for several pages. For example the home page holds the list of blogs and it should show a maximum of 12 items per page. It is not that difficult to implement. The home page view function requires a page_number that defaults to 1 if it not specified:
@pages_blueprint.route('/', defaults={'page_number': ...
Developing Ubuntu Touch apps with Python using pyOtherSide
18 November 2019 0
Peter
Probably many of you know only two mobile phone operating systems, iOS on Apple phones, and Googles Android on all other phones (some 80%). But there is also Ubuntu Touch (UT) originally developed by Canonical Ltd. and continued by UBports. I write a post about this because I am using UT on a Nexus 5 and wondered how difficult it would be to develop apps for it. When I discovered that you can use Python ...
How to set the timezone when using the Python Alpine Docker image
13 November 2019 0
Peter
While writing an application on my Docker platform I hit an unexpected problem, yes, another one ... This was the first time that I had to show the time on a page of a website running on Docker and it appeared that the time was UTC instead of local time. In the code I was just using the normal Python calls to get the time:
import datetime
...
now = datetime.datetime.now()
hour = now.hour
...
Flask multilanguage processing, switching and the 404 Page Not Found exception
13 November 2019 0
Peter
How to implement Flask multilanguage is explained in the Flask docs, see links below. But this is just a starting point. You need deeper understanding of the process to handle special cases like falling back to a default language, language switching, and the 404 Page Not Found exception.
Assumptions
In the remainder of this post we are using a language code, 'lang_code', that is available in the ...
ISPConfig: running a Python Flask Docker container as a jailed Shell User
18 October 2019 0
Peter
I run a server with ISPConfig with some 50 sites. The sites are static or PHP. I am developing Python Flask applications now and also want to run them on the ISPConfig server. You can create virtual environments on the ISPConfig server and run your application from here. But some time ago I have chosen to use Docker for development, staging and production. It takes time to set this up ...
Two Flask apps, frontend and admin, on one domain using DispatcherMiddleware
9 October 2019 3
Peter
The Flask application I am writing to run this website has all code in a single 'app'. I already did some reorganizing as I wanted full separation of frontend code and the administration code. Now it it is time for total separation, meaning making the frontend a Flask app and the admin another Flask app while both running in the same domain and both reside in the same project directory. Because we ...
Adding a contact form to a multilanguage page with content from a database
28 September 2019 0
Peter
Update 11 October 2019: I changed the addon-tag from '{% addon: ... %}' to '[[ addon: ... ]]'. The reason is that I wanted to be able to render the page text coming from the database, using render_template_string, and '{% ... %}' conflicts with Jinja2 tags. And yes, I do not want to implement a Jinja2 custom tag.
What is difficult about implementing a contact page with a contact form with Flask ...
Adding url_for() links to Jinja templates of a Flask multilanguage website
15 September 2019 0
Peter
Before you read on, you may want to read my previous posts about multilanguage and language fallback, see links below. When I gave myself the assignment to develop and implement a multilanguage Flask website with SQLAlchemy I knew things could get difficult. I did not really took the time to design everything beforehand, I just read a lot about multilanguage on the internet and trusted my experience ...
Multilanguage fallback revisited and a page footer with multilanguage links
14 September 2019 0
Peter
Before you read on, you may want to read my previous post about language fallback, see link below. Here you have an example of the complexity of multilanguage. The link should be shown in the language you are using at the moment ... another time.
Life is one long big sequence of problems we are solving all the time. It starts with getting enough oxygen, solved by breathing, maybe second ...