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

  • Home
  • SEARCH
  • 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 4120680
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:15:13+00:00 2026-05-20T23:15:13+00:00

I want to set the initial date as yesterday in a django form my

  • 0

I want to set the initial date as yesterday in a django form my code is here:

class Bilag(models.Model):
dato = models.DateField()
tekst = models.CharField(max_length=100)
konto = models.CharField(max_length=10)
avd = models.CharField(max_length=10, null=True,blank=True)
avdnavn = models.CharField(max_length=30, null=True,blank=True)
kasseid = models.CharField(max_length=10)
belop = models.FloatField()
def __unicode__(self):
    return self.tekst

class BilagForm(ModelForm):
class Meta:
    model = Bilag
    widgets = {
        'dato': SelectDateWidget()
    }
    initial = {
        'dato': yesterday()
    }

and the yesterday function:

def yesterday():
    yesterday = (datetime.date.today() - datetime.timedelta(1))
    return yesterday

But it just displays todays date when i look at the form

  • 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-20T23:15:13+00:00Added an answer on May 20, 2026 at 11:15 pm

    You could set the initial value in the ModelField, though it would then be called default. I assume you only want to do it on the form, in which case you’d need something like:

    class BilagForm(forms.ModelForm):
        dato = forms.DateField(widget=SelectDateWidget(), initial=yesterday)
        class Meta:
            model = Bilag
    

    Don’t forget that you can’t include the parentheses after yesterday — just pass the callable, otherwise yesterday() will be evaluated immediately and not be dynamic (see the bottom of this section).

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

Sidebar

Related Questions

I have a form and want to set the initial focus to a text
I've extended Django's contrib.auth.models user like: class UserProfile(models.Model): user = models.ForeignKey(User, related_name = 'profile')
i want to set my array key initial value to a certain number. here
Ok so I have an OpenFileDialog and I want to set the initial directory
I don't want to give an initial value. I want to set these later
I want to check if the model attributes make_initial_plan, make_default_plan going to be set
i want set class for repeater when doPostBack for those dataID i wan but
To my surprise, I can't easily find a solution. I want to set initial
I want to set my WPF window's initial client size. I'm not seeing a
What I want to do is set the initial zoom scale [and in some

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.