Changes between Version 1 and Version 2 of CookBookManipulatorWithHiddenFields


Ignore:
Timestamp:
Aug 26, 2005, 1:05:31 PM (19 years ago)
Author:
Adam Endicott <leftwing17@…>
Comment:

Updated for new model syntax (note that _id has been removed from many places in the view and template)

Legend:

Unmodified
Added
Removed
Modified
  • CookBookManipulatorWithHiddenFields

    v1 v2  
    99#!python
    1010class 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)
    1715}}}
    1816Here'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
     
    2725        # and replacing them with HiddenFields
    2826        for field in self.fields:
    29             if field.field_name in ('user_id',):
     27            if field.field_name in ('user',):
    3028                field = formfields.HiddenField(field.field_name,
    3129                                               field.is_required,
     
    5048        # Add the the data that you already know and don't want to burden
    5149        # the user with here.
    52         newData = {'user_id': request.user.id,}
     50        newData = {'user': request.user.id,}
    5351    form = formfields.FormWrapper(manipulator, newData, errors)
    5452    t = template_loader.get_template('addMessage')
    5553    c = Context(request, {'form': form,})
    5654    return HttpResponse(t.render(c))
     55
    5756}}}
    5857Here'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.
    5958{{{
    6059<form method="post" action=".">
    61 {{ form.user_id }}
     60{{ form.user }}
    6261<p>
    6362    <label for="id_title">Title:</label> {{ form.title }}
     
    7271{{{
    7372<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" />
    7574<p>
    7675    <label for="id_title">Title:</label> <input type="text" id="id_title"
Back to Top