models.py

class Settings(models.Model):
    date_format = models.CharField('Date format', max_length=100)
    time_format = models.CharField('Time format', max_length=100)

views.py for saving the date and time format in database as boolean.

def date_format(request):

settings = Settings.objects.get(user=request.user)
settingsForm = SettingsForm(instance=settings)  
if request.method =='POST':     
    settingsForm = SettingsForm(request.POST,instance=settings)
    if (settingsForm.is_valid()):  
        settings=settingsForm.save()      
        return redirect('/member/follow-up/') 


return render_to_response( 'incident/date_format.html',
              {
               'about_menu': True,
               'SettingsForm':settingsForm,

             },
             context_instance=RequestContext(request))

forms.py

Date_Format = (
    ('0', ' dd / mm / yyyy'),
    ('1', 'mm / dd / yyyy'),
)

Time_Format = (
    ('0', ' 12 hour AM / PM '),
    ('1', ' 24 hour '),
)
class SettingsForm(forms.ModelForm):
    date_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Date_Format)
    time_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Time_Format)
    class Meta:
        model = Settings
        fields = ['date_format','time_format']

The above view code is to save selected format in database in Boolean format.

In settings page i am having date format like this ![like this][1] and time format is of following type![like this][2] .

So if the user select any one of these format in settings page and save it,saved format will be displayed in report creation page for report add/updation.

The concept is user is selecting the time format,depend on the format they selected the format should change in report creation page like this ![report creation page format][3]

I am searching this in google but no sucess.

I want to know is it possible in django. How to implement this in views.

I learning django so please help me in doing this.

Thanks

Dani AI

Generated

A simple, reliable pattern is: persist a stable preference (an id or the actual format string), map that to the real format you need at render/input time, then format/parse accordingly. This follows the same idea showed with an array lookup, but avoids duplicating view logic: map the stored choice to either Python strftime strings for server-side formatting or to Django template-format strings if you prefer template formatting. , use this at render time so the report page always shows the user’s selected format.

Example mapping + server-side formatting (keep mapping in a module so it’s reusable):

# mapping.py
DATE_FMT_PY = {'0': '%d/%m/%Y', '1': '%m/%d/%Y'}
TIME_FMT_PY = {'0': '%I:%M %p',   '1': '%H:%M'}

# view snippet
dt = timezone.localtime(report.created_at)            # use timezone-aware conversion
fmt = DATE_FMT_PY.get(user_settings.date_format, '%Y-%m-%d')
tf  = TIME_FMT_PY.get(user_settings.time_format, '%H:%M')
context['created_at_display'] = dt.strftime(fmt + ' ' + tf)

For form input/editing, set input_formats and the widget format so Django can parse what the user types (or so your JS picker and the input agree):

# forms.py (inside __init__)
if user_settings.date_format == '0':
    self.fields['date'].input_formats = ['%d/%m/%Y']
    self.fields['date'].widget = forms.DateInput(format='%d/%m/%Y')
# same idea for time (use '%I:%M %p' vs '%H:%M')

Notes and cautions: keep date/time values timezone-aware (django.utils.timezone.localtime). Django template date filter uses different tokens than strftime (e.g. Python '%d' -> Django 'd', '%M' -> 'i', '%p' -> 'A'); either format in the view with strftime or store a Django-format string and use {{ value|date:user_format }}. If you use a JS picker, pass a matching format string to it and ensure server input_formats match the picker output. Test both display and parsing for ambiguous formats (dd/mm vs mm/dd).

NSArray *dateFormats = [NSArray arrayWithObjects: @"dd/ mm / yyyy", @"mm / dd/ yyyy", nil];

NSString *selectedDateFormat = [dateFormats objectAtIndex:formatSelected];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.