in Education by
I want to create a form that shows 2 different textboxes with a minimum and a maximum value of my model's price field, which is a DecimalField. But I'm not sure where to begin. I know I can calculate the min and max value, but I'm not sure how to add that to the placeholder and/or value text. So for right now, I'm just using the view to push the values, but they won't submit in the form. Here is my code so far: forms.py class ProductSearchForm(forms.ModelForm): price_min = forms.DecimalField() price_max = forms.DecimalField() def __init__(self, *args, **kwargs): super(ProductSearchForm, self).__init__(*args, **kwargs) self.fields['length_range'].empty_label = "any size" self.fields['hull_type'].empty_label = "any type" self.fields['power'].empty_label = "any type" self.fields['speed'].empty_label = "any speed" self.fields['hull_only_available'].empty_label = None self.fields['price_min'].widget.attrs['min'] = kwargs['price']['price__min'] self.fields['price_max'].widget.attrs['max'] = kwargs['price']['price__max'] class Meta: model = Product fields = ('length_range', 'hull_type', 'price', 'power', 'speed', 'hull_only_available') views.py class IndexView(FormView): template_name = 'index.html' form_class = ProductSearchForm success_url = "search/" def get_price(self): price = getattr(self, "_price", None) if price is None: price = Product.objects.all().aggregate(Min('price'), Max('price')) setattr(self, "_price", price) return price def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) context['length_ranges'] = LengthRange.objects.all().order_by('pk') context['hull_types'] = Hull.objects.all().order_by('pk') context['power_configs'] = PowerConfiguration.objects.all().order_by('pk') context['speed_ranges'] = SpeedRange.objects.all().order_by('pk') return context def get_form_kwargs(self): form_kwargs = super(IndexView, self).get_form_kwargs() form_kwargs['price'] = self.get_price() return form_kwargs index.html A boat with a length of {{ form.length_range }} , with hull type of {{ form.hull_type }} with {{ form.power }} power configuration and a top speed between {{ form.speed }}. My budget is from $ {{ price__min|intcomma }}"/> to $ {{ price.price__min|intcomma }}"/>. Hull only availability a concern.
EDIT Now I'm getting a TypeError: __init__() got an unexpected keyword argument 'price' Seems to be coming from this line in views.py context = super(IndexView, self).get_context_data(**kwargs) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Sorry, ignore my previous answer, I completely misunderstood your question. I think the problem here is that you are creating a form with only the fields that are contained in Product. However you need a form that contains MORE fields. There is nothing that limits you to only using fields that are in the model. So the idea would be to ignore price (which is a specific price), and instead add price_min and price_max: class ProductSearchForm(forms.ModelForm): price_min = form.DecimalField() price_max = form.DecimalField() def __init__(self, *args, **kwargs): # Pop the price argument, as the parent form doesn't know about it! price = kwargs.pop("price") super(ProductSearchForm, self).__init__(*args, **kwargs) # ... existing code... self.fields['price_min'].widget.attrs['min'] = price['price__min'] self.fields['price_min'].widget.attrs['max'] = price['price__max'] self.fields['price_max'].widget.attrs['min'] = price['price__min'] self.fields['price_max'].widget.attrs['max'] = price['price__max'] You can use these fields in your template as any other regular field. You can then pass the current price values as you kind of already attempting to do, via get_form_kwargs: class IndexView(FormView): # ... existing code... def get_form_kwargs(self): form_kwargs = super(IndexView, self).get_form_kwargs() form_kwargs['price'] = Product.objects.all().aggregate(Min('price'), Max('price')) return form_kwargs Extra suggestion: If you want to avoid making a query to Product twice to get the min/max, then you can create a method that caches the result, like: (in view) def get_price(self): price = getattr(self, "_price", None) if price is None: price = Product.objects.all().aggregate(Min('price'), Max('price')) setattr(self, "_price", price) return price and then you can call self.get_price() to populate the form kwargs and the template context.

Related questions

0 votes
    Suggestions needed for creating better and efficient search indexes for models having foreign key and many-to-many ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I'm stuck with this error for one on my app : Traceback (most recent call last): File "manage.py ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have two tables in my database which are relevant for this problem: exercise_state with following fields: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have configured my timezone in settings, as America/Guatemala and I have some datetime fields in my ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I'm traying to make some tests for an api here is the code: tests.py class APITest(APITestCase): ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have been researching how to create django react application and the best way to go about implementing it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have been researching how to create django react application and the best way to go about implementing it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I have a model with two foreign keys to create many to many relationship - I am not using many to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I have a model with two foreign keys to create many to many relationship - I am not using many to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    So, I started learning to code in Python and later Django. The first time it was hard looking at tracebacks and ... your Django code? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    In the Django model QuerySets, I see that there is a __gt and __lt for comparative values, but is there a __ne/!= ... (a=true, x__gt=5) Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
...