2013-03-11 45 views
3

我的问题是关联到这个链接:How to allow user to input comma如何在通过表单验证之前获取表单字段值?

所以我试图做所有的可能性,只是为了使它的工作。现在我试图在通过form.is_valid()进行验证之前执行保存操作时获取值传递。

我顺利拿到由这样的值:

.........  
if request.method == 'POST': 
    if request.POST['process'] == 'addtrans': 
     tform = AddTransactionForm(request.user, 
            request.POST) 

     print tform.fields['amount'] // this is the value that I want to get 

     if tform.is_valid(): 
.......... 

但不幸的输出是这样的:

<django.forms.fields.DecimalField object at 0x7fcc84fd2c50> 

如何获得准确值或解码该输出?我希望有人试过这个。

+0

您将通过'request.POST.get('amount')'获得原始输入。 'tform.fields'将为这些值提供适当的对象。 – Rohan 2013-03-11 06:15:25

+0

是的,我已经这样做,我得到的金额。我只想得到确切的参数,因为我想用它来替换当前值通过POST – catherine 2013-03-11 06:18:42

+0

@Rohan你知道用其他方式替换POST传递的值吗? – catherine 2013-03-11 06:20:43

回答

3

我觉得这是你描述 -

def transaction(request): 
    if request.POST.method == 'POST': 
     post = request.POST.copy() 
     if 'amount' in post: 
      post['amount'] = post['amount'].replace(',','') 
     tform = AddTransactionForm(request.user, post) 
     #... 

(你必须创建request.POST字典的副本,因为它是不可变的)。

+0

哇,它的工作。最后一个问题,这是安全的吗? – catherine 2013-03-11 07:30:25

+0

是的。真高兴你做到了。 – 2013-03-11 07:38:10