向django表单添加新字段

向django表单添加新字段,django,django-models,django-forms,django-views,Django,Django Models,Django Forms,Django Views,我是Django的新手,我第一次尝试使用Django表单。我已经搜索过了,但还没有找到确切的答案。基本上我有这样的观点: def pay(request): if request.method == 'POST': form = PaymentForm(request.POST) if form.is_valid(): # I have to calculate the checksum here myModel = form.save()

我是Django的新手,我第一次尝试使用Django表单。我已经搜索过了,但还没有找到确切的答案。基本上我有这样的观点:

def pay(request):    
if request.method == 'POST':
    form = PaymentForm(request.POST)
    if form.is_valid():
        # I have to calculate the checksum here
        myModel = form.save()
    else:
        print form.errors
else: # The request is GET  
    form = PaymentForm()
return render_to_response('payment/payment.html', {'form':form})
class PaymentModel(models.Model):
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed!')
secret_key = '6cd118b1432bf22942d93d784cd17084'
pid = models.CharField(primary_key=True, max_length=50, validators=[alphanumeric])
sid = models.CharField(primary_key=True, max_length=50, validators=[alphanumeric])
amount = models.DecimalField(max_digits=9, decimal_places=2)
success_url = 'http://localhost:8000/success'
cancel_url = 'http://localhost:8000/cancel'
error_url = 'http://localhost:8000/error'
checksum = 0

def calc_checksum(self):
    checksumstr = "pid=%s&sid=%s&amount=%s&token=%s"% (self.pid, self.sid, self.amount, self.secret_key)
    m = md5(checksumstr)
    checksum = m.hexdigest() 
    return checksum

def __unicode__(self): #returns the unicode representation of the object
    return self.name
class PaymentForm(ModelForm):
    class Meta:
        model = PaymentModel
我还想添加一个额外的字段,从表单中获取的输入将校验和添加到表单中,因此当用户提交条目时,校验和应该添加到表单中,表单应该发送到外部服务器。但我不知道怎么做(我在模型中定义了校验和)。有人能帮我吗

我的模型如下所示:

def pay(request):    
if request.method == 'POST':
    form = PaymentForm(request.POST)
    if form.is_valid():
        # I have to calculate the checksum here
        myModel = form.save()
    else:
        print form.errors
else: # The request is GET  
    form = PaymentForm()
return render_to_response('payment/payment.html', {'form':form})
class PaymentModel(models.Model):
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed!')
secret_key = '6cd118b1432bf22942d93d784cd17084'
pid = models.CharField(primary_key=True, max_length=50, validators=[alphanumeric])
sid = models.CharField(primary_key=True, max_length=50, validators=[alphanumeric])
amount = models.DecimalField(max_digits=9, decimal_places=2)
success_url = 'http://localhost:8000/success'
cancel_url = 'http://localhost:8000/cancel'
error_url = 'http://localhost:8000/error'
checksum = 0

def calc_checksum(self):
    checksumstr = "pid=%s&sid=%s&amount=%s&token=%s"% (self.pid, self.sid, self.amount, self.secret_key)
    m = md5(checksumstr)
    checksum = m.hexdigest() 
    return checksum

def __unicode__(self): #returns the unicode representation of the object
    return self.name
class PaymentForm(ModelForm):
    class Meta:
        model = PaymentModel
我的表格如下所示:

def pay(request):    
if request.method == 'POST':
    form = PaymentForm(request.POST)
    if form.is_valid():
        # I have to calculate the checksum here
        myModel = form.save()
    else:
        print form.errors
else: # The request is GET  
    form = PaymentForm()
return render_to_response('payment/payment.html', {'form':form})
class PaymentModel(models.Model):
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed!')
secret_key = '6cd118b1432bf22942d93d784cd17084'
pid = models.CharField(primary_key=True, max_length=50, validators=[alphanumeric])
sid = models.CharField(primary_key=True, max_length=50, validators=[alphanumeric])
amount = models.DecimalField(max_digits=9, decimal_places=2)
success_url = 'http://localhost:8000/success'
cancel_url = 'http://localhost:8000/cancel'
error_url = 'http://localhost:8000/error'
checksum = 0

def calc_checksum(self):
    checksumstr = "pid=%s&sid=%s&amount=%s&token=%s"% (self.pid, self.sid, self.amount, self.secret_key)
    m = md5(checksumstr)
    checksum = m.hexdigest() 
    return checksum

def __unicode__(self): #returns the unicode representation of the object
    return self.name
class PaymentForm(ModelForm):
    class Meta:
        model = PaymentModel

您可以使用
commit=False
关键字参数来
form.save()


您需要将此字段添加到
付款表单中
…您可以更新您的问题并将表单结构添加到其中,而不是将其作为注释发送。另外,你最好发布你的
PaymentModel
,因为你使用了
ModelForm
我按照你的建议编辑了这个帖子:)不幸的是,它仍然不起作用。我添加了模型和表单。有可能知道原因吗?我不清楚你的问题是什么。是否要将校验和存储在数据库中?否我要将校验和与其他数据(pid、sid、金额等)打包,并将它们全部发送到需要这些字段的外部服务器。