How do you set a flash message in the template in Craft 3?
You can set flash messages using yii\web\Session::setFlash().

The session component is available to templates via craft.app.session.



{% do craft.app.session.setFlash('error', 'You’re not allowed to go there.') %}


For convenience, Craft’s SessionBehavior class adds shortcut setError() and setNotice() methods to the session component as well.

{% do craft.app.session.setError('You’re not allowed to go there.') %}


You can place that right before your {% redirect %} tag.