-1

Given a FormView associated to a Form, once the form is submitted some fields are sent to the FormView (those the user has filled).

Question: How can I send to the FormView other fields even if the user didn't fill them (because the fields were already prepopulated)? I need more data than the user provides in order to be used in form_valid

loar
  • 1,505
  • 1
  • 15
  • 34
  • What do you mean by *send[ing fields] to the FormView*? If I understand what you want to do correctly, you can use `get_initial` (https://docs.djangoproject.com/en/1.9/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.get_initial) function of the `FormView` class to pre-populate your form. If you don't want those fields to be displayed to the user, hide them in you `Form` definition. Here is example usage: http://stackoverflow.com/questions/22083218/django-how-to-pre-populate-formview-with-dynamic-non-model-data – Siegmeyer Aug 03 '16 at 14:27
  • @Siegmeyer I mean... I have a prepopulated form, users fill the fields they want and the data is sent to the FormView. Besides that, I want an specific field of the form is always sent with the rest of the data, no matter if a user has filled this specific field or not (has the default value from prepopulated step) – loar Aug 03 '16 at 14:33
  • All fields from your `Form` definition are send in `request.POST` by default, no matter if they are hidden, pre-populated, filled by user or left untouched. After form validation you can all access them with `form.cleaned_data`. – Siegmeyer Aug 03 '16 at 14:42

1 Answers1

0

If the data is not sensitive you might want to add them to your form as hidden fields. In django you can achieve this with a FormWidget. There are a few related questions here on SO:

Be aware that this just hides your data in the user interface, not in the browser - if you are in trouble if someone will change this data in an evil matter, you might consider writing the data in the session.

Community
  • 1
  • 1
dahrens
  • 3,879
  • 1
  • 20
  • 38