Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 401607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:02:58+00:00 2026-05-12T17:02:58+00:00

I’m trying to find an easy way to build forms which show dates in

  • 0

I’m trying to find an easy way to build forms which show dates in the Australian format (dd/mm/yyyy). This was the only way I could find to do it. It seems like there should be a better solution.

Things to note:

  • Created a new widget which renders date value in dd/mm/yyyy format
  • Created new date field to prepend the locate date format string to the list of values it tries to use

I imagine the ideal solution would be a datefield which automatically localised but that didn’t work for me (strftime didn’t seem to be unicode friendly but I didn’t try very hard)

Am I missing something? Is there a more elegant solution? Is this a robust approach?

from models import *
from django import forms
import datetime

class MyDateWidget(forms.TextInput):

    def render(self, name, value, attrs=None):

            if isinstance(value, datetime.date):
                    value=value.strftime("%d/%m/%Y")

            return super(MyDateWidget, self).render(name, value, attrs)


class MyDateField(forms.DateField):

    widget = MyDateWidget

    def __init__(self, *args, **kwargs):
            super(MyDateField, self).__init__(*args, **kwargs)
            self.input_formats = ("%d/%m/%Y",)+(self.input_formats)


class ExampleForm(ModelForm):
    class Meta: 
            model=MyModel
            fields=('name', 'date_of_birth')

    date_of_birth = MyDateField()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:02:59+00:00Added an answer on May 12, 2026 at 5:02 pm

    Granted, I’m not a django/python Jedi, but…

    I don’t think there is anything wrong with your approach. It is a clean read.

    You might not need to create your own widget just to render the date in the proper format on the first form display. Just use the format parameter for the DateInput widget and you should be OK. So in your MyDateField class I would just do:

    class MyDateField(forms.DateField):
    
        widget = forms.DateInput(format="%d/%m/%Y")
    
        def __init__(self, *args, **kwargs):
            super(MyDateField, self).__init__(*args, **kwargs)
            self.input_formats = ("%d/%m/%Y",)+(self.input_formats)
    

    You could use formfield_callback (See the accepted answer for this distantly related question), meaning:

    def customize_fields(f):
        if isinstance(f, DateTimeField):
            datetime_field=forms.DateTimeField(widget=
                forms.DateInput(format='%d/%m/%Y %h:%i'))
            date_field.input_formats = ("%d/%m/%Y %h:%i",)+
                (date_field.input_formats)
            return datetime_field
        elif isinstance(f, DateField):
            date_field=forms.DateField(widget=
                forms.DateInput(format='%d/%m/%Y'))
            date_field.input_formats = ("%d/%m/%Y",)+
                (date_field.input_formats)
            return date_field
        else:
            return f.formfield()
    
    class MyModelForm(forms.ModelForm):
        formfield_callback = customize_fields
    
        class Meta:
            model = ...
    

    Doing that completely eliminates the need for your MyDateField class, but might introduce – if you want every single form class to call customize_fields – the need for a ModelForm descendent, implementing formfield_callback=customize_fields as the new base for all your form classes.

    My problem with using the formfield_callback mechanism is two-fold:

    1. It is less readable and relies in knowledge of the inner workings of ModelForms. I can’t find actual documentation on formfield_callback anywhere…

    2. If you ever need to redefine a model field in a ModelForm, say to make the field not required for that particular instance of the form, like so:

         class MyModelForm(forms.ModelForm):
             formfield_callback = customize_fields
             foo = forms.DateField(required=false)
      
             class Meta:
                 model = Bar
      

      you are overwriting the form field returned by customize_fields, hence completely losing the customization.

    I think your approach is more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.