python - How to make django crispy form to hide a particular field? -


i'm trying make date_modified field hidden since have passed datetime.now parameter on defining date_modified field in model.

model.py

class guide(models.model):     name = models.charfield(max_length=50)     sno = models.charfield(max_length=50)     date_created = models.datetimefield(default=datetime.now, blank=true)     date_modified = models.datetimefield(default=datetime.now, blank=true)      def __unicode__(self):         return unicode(self.name) 

views.py

class guideformupdateview(updateview):     model = guide     fields = ['name', 'sno', 'date_modified']     template_name_suffix = '_update_form'     success_url = reverse_lazy('guides') 

corresponding form forms.py looks like

<form role="form" method="post" action="{% url 'guideform-edit' object.pk %}"               class="post-form form-horizontal" enctype="multipart/form-data">{% csrf_token %} {{ form|crispy }} <button type="submit" value="upload" class="save btn btn-default btn-primary center-block">update</button>          </form> 

this form displays date_modified field. don't want field on frontend instead want value of field in model or db_table should updated. know how hide particular field in jquery don't want touch js tools. there way make crispy exclude particular field {{ form|crispy|exclude:date_modified }} ..

instead of using generic form updateview use implicitly, create custom form. , in custom form change widget of date_modified field.

in forms.py

from django.forms import modelform, hiddeninput class guideform(modelform):     def __init__(self, *args, **kwargs):         super(guideform, self).__init__(*args, **kwargs)         self.fields['date_modified'].widget = hiddeninput()      class meta:         fields = ('name', 'sno', 'date_modified', )         model = models.guide 

in views.py

class guideformupdateview(updateview):     model = guide     form_class = forms.guideform     template_name_suffix = '_update_form'     success_url = reverse_lazy('guides') 

to automatically update date_modified whenever update record, need use attributes auto_now , auto_now_add instead of default. see docs. model be

class guide(models.model):     name = models.charfield(max_length=50)     sno = models.charfield(max_length=50)     date_created = models.datetimefield(auto_now_add=true, blank=true)     date_modified = models.datetimefield(auto_now=true, blank=true)      def __unicode__(self):         return unicode(self.name) 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -