form = SQLFORM.factory(Field('description', 'string', requires=IS_NOT_EMPTY()), Field('customer', requires=IS_IN_DB(db((db.t_customer.f_company == session.company)&(db.t_customer.f_internal != True)&(db.t_customer.f_billable == True)), 't_customer.id', '%(f_internal_name)s %(f_default_ho Field('employee', requires=IS_IN_DB(db((db.t_customer.f_company == session.company)&(db.t_customer.f_internal == True)), 't_customer.id', '%(f_internal_name)s')), Field('product', requires=IS_EMPTY_OR(IS_IN_DB(db((db.t_product.f_company == session.company)&(db.t_product.f_frontpage == True)&(db.t_product.f_type == 'product')), 't_product.id', '%(f_internal_name)s'))), Field('hour', requires=IS_EMPTY_OR(IS_IN_DB(db((db.t_product.f_company == session.company)&(db.t_product.f_type == 'hour')), 't_product.id', '%(f_internal_name)s'))), Field('value', 'double', default = 1, requires=IS_NOT_EMPTY()),)

    form.vars.employee = session.worker

    if form.accepts(request.vars, session):

        adding_hour = 0
        adding_product = 0

        for var in form.vars:

            if (var == 'product')&(form.vars[var] != None):
                adding_product = 1
                response.flash=T('Product added')
            if (var == 'hour')&(form.vars[var] != None):
                adding_hour = 1
                response.flash=T('Hours added')

        if adding_hour & adding_product:
            response.flash = T('Choose either a product or hour')
            redirect(URL('default', 'index'))

        if adding_product:
            product_id = form.vars.product
        elif adding_hour:
            product_id = form.vars.hour
        else:
            response.flash = T('Choose either a product or hour')
            redirect(URL('default', 'index'))

        new = db.t_sold_products.insert(**{
            'f_name': form.vars.description,
            'f_amount': form.vars.value,
            'f_customer': form.vars.customer,
            'f_worker': form.vars.employee,
            'f_product': product_id,
                    })
        redirect(URL('default', 'index'))

    elif form.errors:
        response.flash="form error"

    return dict(form = form)


The view:
{{extend 'layout.html'}}


<div id="front_left">
<h2>News:</h2>
<br>
<h2>TODO</h2>
<h2>Issues:</h2>
</div>


<div id="front_right">

{{=form.custom.begin}}
<table>
<tr><td>{{=T(form.custom.label.description)}}</td><td>{{=form.custom.widget.description}}</td></tr>
<tr><td>{{=T(form.custom.label.customer)}}</td><td>{{=form.custom.widget.customer}}</td></tr>
<tr><td>{{=T(form.custom.label.employee)}}</td><td>{{=form.custom.widget.employee}}</td></tr>
<tr><td>{{=T(form.custom.label.hour)}}</td><td>{{=form.custom.widget.hour}}</td></tr>
<tr><td></td><td>{{=T('Choose either product or hour')}}</td>
<tr><td>{{=T(form.custom.label.product)}}</td><td>{{=form.custom.widget.product}}</td></tr>
<tr><td>{{=T(form.custom.label.value)}}</td><td>{{=form.custom.widget.value}}</td></tr>
<tr><td></td><td>{{=form.custom.submit}}</td></tr>
</table>
    {{=form.custom.end}}

</div>


Kenneth

we need to see the code to be able to answer.

On Jan 21, 4:07 pm, Kenneth Lundström<[email protected]>
wrote:
I have a form created with factory.

After creation I define form.vars.employee = session.worker

This value is the default when viewing the form. I submit the form and
in if form.accepts I have a redirect to the same page. I want to have a
new empty form with this one fields predefined, but it s empty. Only if
I reload the page the field is occupied. Why is this happening?

Should not a redirect reload the whole page?

Kenneth

Reply via email to