Changes between Version 1 and Version 2 of CookBookManipulatorWithHiddenFields
- Timestamp:
- Aug 26, 2005, 1:05:31 PM (19 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
CookBookManipulatorWithHiddenFields
v1 v2 9 9 #!python 10 10 class Message(meta.Model): 11 fields = ( 12 meta.CharField('title', maxlength=255), 13 meta.TextField('body'), 14 meta.DateTimeField('postDate', auto_now_add=True), 15 meta.ForeignKey(auth.User), 16 ) 11 title = meta.CharField(maxlength=255) 12 body = meta.TextField() 13 postDate = meta.DateTimeField(auto_now_add=True) 14 user = meta.ForeignKey(auth.User) 17 15 }}} 18 16 Here's the custom manipulator that allows us to hide the user and project fields. It inherits from the model's pre-made !AddManipulator, the iterates over the fields replacing some of them with a formfields.!HiddenField … … 27 25 # and replacing them with HiddenFields 28 26 for field in self.fields: 29 if field.field_name in ('user _id',):27 if field.field_name in ('user',): 30 28 field = formfields.HiddenField(field.field_name, 31 29 field.is_required, … … 50 48 # Add the the data that you already know and don't want to burden 51 49 # the user with here. 52 newData = {'user _id': request.user.id,}50 newData = {'user': request.user.id,} 53 51 form = formfields.FormWrapper(manipulator, newData, errors) 54 52 t = template_loader.get_template('addMessage') 55 53 c = Context(request, {'form': form,}) 56 54 return HttpResponse(t.render(c)) 55 57 56 }}} 58 57 Here's part of the template code. {{ form.user_id }} places the hidden field in the form. It will be populated with the correct data because we set that in newData above. Title and Body are other fields defined in the Message model. messages.!AddManipulator took care of populating those correctly. 59 58 {{{ 60 59 <form method="post" action="."> 61 {{ form.user _id}}60 {{ form.user }} 62 61 <p> 63 62 <label for="id_title">Title:</label> {{ form.title }} … … 72 71 {{{ 73 72 <form method="post" action="."> 74 <input type="hidden" id="id_user _id" name="user_id" value="2" />73 <input type="hidden" id="id_user" name="user" value="2" /> 75 74 <p> 76 75 <label for="id_title">Title:</label> <input type="text" id="id_title"