Make all links start with "https://example.com/" in HTML

I create HTML which I will send via email.

The first version was broken, because Foo does not work in
an email :slight_smile:

Since there are several hyperlinks, I would like to keep the method which creates
the HTML the way it is.

How to make all hyperlinks in HTML include “https://example.com/”?

There are some hyperlinks which already have a full url …

If you are sending an email as a part of an HTML request, you can generate the full urls using that request via request.build_absolute_uri().

If you’re sending an email without a request, such as a background task, you’ll need to identify the domain somehow. You could use an environment variables or you could use the django.contrib.sites framework. Here’s the specific bit of the docs on generating the URL using the sites framework. I suspect there are other options available, but those are the two I have typically reached for.

I normally use the sites framework, and ensure it’s properly configured.

Another option is to use an enviroment variable to set a custom setting that is “the site’s domain”, and use that to populate ALLOWED_HOSTS, your emails, etc. This avoids the database updates involved in using the sites framework.