Monday, July 30, 2012

Handling Forms in Django, Once-and-for-all

    def request(self, request, *args, **kwargs):


        self.request = request
        self.view_args = args
        self.view_kwargs = kwargs

        data = getattr(request, request.method)
        self.form_name = data.get('frm')
        self.button_name = data.get('btn')


        self.build_breadcrumbs() #TODO: SLOTTED IN THIS HERE.
###################


        if request.method == 'POST':
            self.product_edit_form = ProductEditForm(self.get_submit_data(), instance=self.product)
            self.product_edit_form.fields['lang'].choices = [
                (code, get_localized_desc(code)) for code in LANGUAGE_CODES
            ]
            self.product_edit_form.fields['lang'].help_text = '(Select language to refresh fields to the new language)'
            # Construct the EXACT entire form again - if not, it will complain "en is not one of the available choices"       Select a valid choice. en is not one of the available choices.

            print "submitted"
            # if errors, you end up at the same form here again. With "this field is required" error.
            # if no errors, you also end up submitted here unless you "return redirect ..." -- but with no error messages on the form!
        else:
            self.create_product_edit_form()
            print "new form"








No comments:

Post a Comment