[Flask] setting SERVER_NAME for only CLI commands

Corey Boyle coreybrett at gmail.com
Thu Oct 28 19:44:01 EDT 2021


Not sure if this would be considered a hack or not, but I ended up using...

with current_app.test_request_context(base_url="https://www.xyz.com"):
    my_email_func()

... inside my cli function.

I only wanted to set the SERVER_NAME for one particular cli script, so
doing that with my config system didn't seem like the right place.


On Thu, Oct 28, 2021 at 5:22 PM ulrich <ub at artfacts.net> wrote:

> hello,
>
> On 10/28/21 10:18 PM, Corey Boyle wrote:
> > I would like to use url_for with the _external option for generating
> > some links inside emails that a CLI command will generate.
> >
> > If I set the SERVER_NAME config var, it solves this problem nicely,
> > however, it breaks my local dev environment.
>
> how? do you not have a seperate configuration for you production
> environment?
>
> > How would I set the SERVER_NAME only for CLI commands?
>
> if would you like to send emails from your production environment, then
> why don't you employ the "classes and inheritance" pattern, suggested
> here:
> https://flask.palletsprojects.com/en/2.0.x/config/#development-production
>
> or you override SERVER_NAME from the environment:
> https://flask.palletsprojects.com/en/2.0.x/config/#SERVER_NAME
>
> employ dotenv to make that transparent and easy for yourself.
>
> or introduce an option to the cli command, like --server-name to set it
> on a per command basis explicitly.
>
> there might well be more ways and of course, what's best, depends on
> your usecase.
>
>
> best,
>
> ub
>
>
> > _______________________________________________
> > Flask mailing list
> > Flask at python.org
> > https://mail.python.org/mailman/listinfo/flask
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/flask/attachments/20211028/082810f5/attachment.html>


More information about the Flask mailing list