Wednesday, May 31, 2017

Re: value of checkbox aren't saved in the form

Your checkbox fields on the form are "market" and "sector", but your
"Parameters" model doesn't have fields with those names, and you
aren't doing anything with the values from the form in your save()
method.

Where were you expecting those checkboxes to be saved?

Cheers

Tom

On Wed, May 31, 2017 at 1:15 PM, <farah_chaaban@hotmail.com> wrote:
> i am a beginner with Django. I don''t know what the problem with the value
> of checkbox. When i enter to the saved form the value of checkbox aren't
> save.The image is here . Please I need your help, I loose too much time on
> this.
>
> This is my forms.py:
>
> class BacktestForm(forms.ModelForm):
>
> period_start = forms.DateField(initial=datetime.datetime.today().date() -
> datetime.timedelta(days=365+16),
> widget=forms.widgets.DateInput(format="%Y/%m/%d"),
> input_formats=["%Y/%m/%d"])
> period_end = forms.DateField(initial=datetime.datetime.today().date() -
> datetime.timedelta(days=16),
> widget=forms.widgets.DateInput(format="%Y/%m/%d"),
> input_formats=["%Y/%m/%d"])
>
> market =
> forms.MultipleChoiceField(required=False,widget=CheckboxSelectMultiple,
> choices=MARKET_CHOICES)
> sector =
> forms.MultipleChoiceField(required=False,widget=CheckboxSelectMultiple,
> choices= MEDIA_CHOICES)
> class Meta:
> model = Parameters
>
> This is my models.py:
>
> class Parameters(models.Model):
>
> user = models.ForeignKey(User)
> title = models.CharField('title', max_length=100, default='', blank=True,
> help_text='Use an indicative name, related to the chosen parameters')
> type = models.CharField('forecast type', choices=FORECAST_TYPES,
> max_length=20, default="backtest")
>
> #input characteristics
> price_1_min = models.FloatField('1. Price, min', default=0.1,
> validators=[MinValueValidator(0.1), MaxValueValidator(20000)])
>
> def get_backtest_url(self):
> return reverse('saved_backtest', kwargs={'pk': self.pk})
>
>
> The save function in views.py:
>
>
> if request.method == 'POST':
> if form.is_valid():
> if 'save' in request.POST:
> obj = form.save(commit= False)
> obj.user = request.user
> obj.type = "backtest"
> obj.save()
> messages.info(request, 'Saved!')
> return redirect(obj.get_backtest_url())
>
>
> The template for the saved form is :
>
> <div> {% if user.is_authenticated %}
> {% if user.profile.is_active %}
> &nbsp; Saved from "Backtesting"
> <ul>
> {% for param in user.parameters_set.all %}
> {% if param.type == "backtest" %}
> <table
> style="width:100%">
> <tr>
> <th> Saved File
> </th>
> <th> Created at
> </th>
>
> <tr>
>
> <td> <a
> href='{{param.get_backtest_url}}'>{{param.title}} </a> </td>
> <td> {{param.created_at}} </td>
>
> </tr>
> </table>
> {% endif %}
> {% endfor %}
> </ul>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/55e259b4-8986-41d0-849e-4fe809001ff0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1KXUD5%3DZRT%2Bu4k3EBLnzC%3D5MLt6LCPO-UX%2BWJrLrAT5wQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment