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 8231657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:34:13+00:00 2026-06-07T17:34:13+00:00

I need an integer form field to have a ‘default’ value. But I need

  • 0

I need an integer form field to have a ‘default’ value. But I need to define this value at runtime.

I do it like ths:

# at my form constructor:
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList,
             label_suffix=':', empty_permitted=False):
    super(FlatSearchForm, self).__init__(data, files, auto_id, prefix, initial, error_class, label_suffix,
        empty_permitted)

    min_price = ... # some code to find the minimum price
    self.price_min = min_price # will be used later

    max_price = ... # some code to find the maximum price
    self.price_max = max_price

    # here we go:
    self.fields['price_from'] = forms.IntegerField(min_value=min_price, max_value=max_price, initial=min_price)
    self.fields['price_to'] = forms.IntegerField(min_value=min_price, max_value=max_price, initial=max_price)

# ...
# how I use the form (in views.py):
class SearchFlatView(ListView):
    model = Flat
    context_object_name = 'flats'
    template_name = 'catalog/search.html'

    def get_context_data(self, **kwargs):
        context = super(SearchFlatView, self).get_context_data(**kwargs)
        context['form'] = FlatSearchForm(self.request.GET)
        return context

The problem is: values are not shown in the form when I render the field using {{form.price_from}} and {{form.price_to}} – these fields are just empty!

Can you please tell me what I am doing wrong?

  • 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-06-07T17:34:15+00:00Added an answer on June 7, 2026 at 5:34 pm

    You cannot set dynamically initial values for fields in a bound form. See the docs: https://docs.djangoproject.com/en/dev/ref/forms/fields/#initial

    The initial argument lets you specify the initial value to use when
    rendering this Field in an unbound Form.

    What you can do however is to create an unbound form, with still preserving the request.GET params:

    initial = request.GET.copy()
    initial['price_from'] = some_min_price
    initial['price_to'] = some_max_price
    context['form'] = FlatSearchForm(initial=initial)
    

    While in the form __iniit__ you’ll have:

    def __init__(self, *args, **kwargs):
        super(Form, self).__init__(*args, **kwargs)
        initial = kwargs.get("initial", {})
        default_min_value = ... #  some default value in case there is no `initial` passed
        default_max_value = ...
        self.fields['price_from'] = forms.IntegerField(min_value=initial.get('min_value', default_min_value), max_value=initial.get('max_value', default_max_value))
        self.fields['price_to'] = forms.IntegerField(min_value=initial.get('min_value', default_min_value), max_value=initial.get('max_value', default_max_value))
    

    Or you may want to change this logic, if needed.

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

Sidebar

Related Questions

I am trying to scrape form field IDs using Beautiful Soup like this for
While replacing a pattern in a string, I specifically need the integer/long value of
I need to show an integer value in a TextBox in my C# Windows
I need to get the left hand side integer value from a decimal or
Hi I have a table in 3NF form ftype_table = Table( 'FTYPE', Column('ftypeid', Integer,
I have a sqlite table that has amount field with NUMERIC(10,5) value(I know data
I want to pass an integer value to a form in .Net so that
I need to use Integer Linear Programming API/Tool for my application. Though my application
i need to set an integer to start from 001 instead of only 1
I need to compare an integer in a loop to the quotient of a

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.