Django ModelForm has hidden input

So, I have a TagStatus model. I am trying to create a ModelForm for this. However, my form requires hidden input to be filled in {{tag.name}}. I was looking through documents and I do not know how to make the tag field hidden. Perhaps ModelForm is not suitable?

models.py:

class TagStatus(models.Model): user = models.ForeignKey(User, null=True, unique=True) status = models.CharField(max_length=2, choices=tag_statuses) tag = models.ForeignKey(Tag, null=True, blank=True) def __unicode__(self): return self.status def save(self, *args, **kwargs): super(TagStatus, self).save(*args, **kwargs) class TagStatusForm(modelForm): class Meta: model = TagStatus fields = ('status','tag') widgets = { 'select': Select, 'tag': ??? } 

django views.py:

 @login_required def tags(request): all_tags = Tag.objects.all() context = base_context(request) if request.method == 'POST': if 'status_check' in request.POST: status_form = TagStatusForm(request.POST) #if request.is_ajax(): if status_form.is_valid(): status_form.save() response = simplejson.dumps({"status": "Successfully changed status"}) else: response = simplejson.dumps({"status": "Error"}) return HttpResponse (response, mimetype='application/json') status_form = TagStatusForm() context['status_form'] = status_form context['all_tags'] = all_tags return render_to_response('tags/tags.html', context, context_instance=RequestContext(request)) 

template:

 {% for tag in all_tags %} .... <form class="nice" id="status-form" method="POST" action=""> {% csrf_token %} <input type="hidden" name="status_check" /> <input type='hidden' name="tag" value="{{ tag.name }}" /> {{ status_form.status }} </form> ... {% endfor %} 

How can I make hidden input through django ModelForm and then fill it through a template?

+56
python django django-models django-forms
Apr 03 '13 at 19:06 on
source share
4 answers

To create a field in the ModelField field in a hidden field, use the HiddenInput widget. ModelForm uses a reasonable default widget for all fields, you just need to override it when creating the object.

 class TagStatusForm(forms.ModelForm): class Meta: model = TagStatus widgets = {'tag': forms.HiddenInput()} 
+99
May 05 '14 at 12:04
source share

There are 3 possible ways (AFAIK) to display hidden fields in Django -

1 .. You can usually declare a field in forms.py , but in your html file template use {{ form.field.as_hidden }}

2. In forms.py directly use the hidden input widget.

 class MyForm(forms.Form): hidden_field = forms.CharField(widget=forms.HiddenInput()) 

Once you make the field hidden, you can fill in the value of the field in the templates. Your hidden field is now ready for rendering.

3. The usual form equivalent (thanks to @Modelesq for sharing this nugget). There is no Django here. Changes are made at the HTML template level. <input type="hidden" name="tag" value="{{ tag.name }}" />

+70
Apr 03 '13 at 19:10
source share

I was looking for a way to MAKE ALL INPUTS:

 class TagStatusForm(forms.ModelForm): class Meta: model = TagStatus def __init__(self, *args, **kwargs): super(TagStatusForm, self).__init__(*args, **kwargs) for field in self.fields: self.fields[field].widget = forms.HiddenInput() 
0
May 29 '18 at 15:37
source share

I posted a way to do this with a generic based class view here :

 from django.forms import HiddenInput from django.forms.models import modelform_factory _patient_create_form = modelform_factory( models.Patient, fields=['name', 'caregiver_name', 'sex', 'birth_date', 'residence', 'country'], widgets={'country': HiddenInput()}) class PatientCreate(LoginRequiredMixin, UserOrgRequiredMixin, CreateView): form_class = _patient_create_form template_name = 'healthdbapp/patient_form.html' 
0
Dec 05 '18 at 16:59
source share



All Articles