Django, allow business admin to set 12/24 hour clock?

Hi everyone I am working through a issue I’ve been dealing with to allow business admin to change their company’s system time clock from either 12 hour to 24, and vice versa. I have tried a couple different ways, but unable to find a way to display the change in the view.

I have 2 views, a basic display View and an UpdateView

class DateTimeConfig(View):

    def get(self, request):
            business = Business.objects.filter(**get instance here)
            business_tz = business.timezone
            # Default to 12 hour clock
            current_time ="%m/%d/%Y, %I:%M%p")
            context = {
                'timezone': business_tz,
                'current_time': current_time
            return JsonResponse(context, status=200)

class DateTimeConfigUpdate(UpdateView):

    model = Business
    form_class = SystemUpdateForm

    def get_object(self, queryset=None):
        business = self.get_queryset().filter(**get instance here)
        return company
    def form_valid(self, form):
        return JsonResponse({'message': 'Business date/time updated successfully.'}, status=200)

I have a simple form here, to change the Timezone and a dropdown for 12, or 24.

class SystemUpdateForm(ModelForm):

    clock = forms.ChoiceField(choices=(('12', '12'), ('24', '24')))

    class Meta:
        model = Business
        fields = ['timezone', 'clock']

    def save(self, commit=True):
        if self.cleaned_data.get('clock') == '24':
            logger.debug('24 hour clock')
            logger.debug('12 hour clock')

Now, I know the form does work, because the appropriate logged message changes based on the 12, and 24 dropdown.

My issue is figuring a way to override the DateTimeConfig view to reflect this change, which I know is due to the default here…

current_time ="%m/%d/%Y, %I:%M%p")

Since that is displaying the default 12 hour clock, I need a way to modify that current_time variable’s %I , to %H if a user does want the 24 hour clock.

Just looking for any recommendations from anyone who has dealt with a similar issue! Thank you!

Probably the most reliable way to maintain this would be to create a settings table where you have the two different format strings. Then, whenever you’re formatting the time, you retrieve the format string from the table and use the active value.