Python/Django Filter with Datetime and extra options
Set your locale globally

I would recommend doing this in your settings.py file or within a global __init__.py file.

locale.setlocale(locale.LC_TIME, "de")


Django template tags allow multiple parameters, use them

Right now you have a single complex parameter which is being used to store two pieces of information: whether to use a sentence and if only the date should be rendered. Since this is passed in as a single string, you have to use the str2bool function from Stack Overflow in order to parse it. You don't need to parse that string if you use separate arguments.

@register.filter
def naturaldaytime(daytime, sentence=False, only_date=False):
    from django.conf import settings

    if daytime is None:
        return daytime


Use descriptive variable names in your comparisons

Right now you have to look into nested calls to understand what comparisons are actually being done. While this still needs to happen, you can make us to intermediate variables which makes it a lot more clear what is happening.

todays_date = datetime.now().date()
yesterdays_date = todays_date - timedelta(days=1)

daytime_week_number = daytime.date().isocalendar()[1]
todays_week_number = todays_date.isocalendar()[1]


Construct the format key you need so you can avoid repetition

Right now you have a complex set of conditional statements that you are using to determine what date format to use. This is resulting in a ton of repetition when it comes to doing the actual formatting, so I would recommend separating the "getting the right format settings" part from the actual "do the formatting".

if not only_date:
    setting_key = "DATETIME_FORMAT"
else:
    setting_key = "DATE_FORMAT"

if daytime.date() == todays_date:
    setting_key += "_TODAY"
elif daytime.date() == yesterdays_date:
    setting_key += "_YESTERDAY"
elif daytime_week_number == todays_week_number:
    setting_key += "_THIS_WEEK"
elif daytime.date().year == todays_date.year:
    setting_key += "_THIS_YEAR"

if sentence:
    setting_key += "_SENTENCE"


This way you don't need to keep repeating the same logic for every "sentence" or "not only the date" variant, and you can keep settings keys consistent at the same time.

Get the right date format using that settings key you already built

This part doesn't change much, except you now need to dynamically retrieve the setting from the settings object.

date_format = getattr(settings, setting_key)

return daytime.strptime(date_format)