我有一个模型:如下所示
class Pockets(models.Model):
pocket_owner = models.CharField(default="None",max_length=50)
pocket_name = models.CharField(primary_key=True,max_length=50)
pocket_description = models.CharField(max_length=500)
pocket_1000 = models.IntegerField(default=0)
pocket_500 = models.IntegerField(default=0)
pocket_100 = models.IntegerField(default=0)
pocket_50 = models.IntegerField(default=0)
pocket_20 = models.IntegerField(default=0)
pocket_10 = models.IntegerField(default=0)
pocket_5 = models.IntegerField(default=0)
pocket_2 = models.IntegerField(default=0)
pocket_1 = models.IntegerField(default=0)
pocket_change = models.FloatField(default=0)
initial_amount = models.FloatField(default=0)
sum_pocket = models.FloatField(default=0)
pocket_total = models.FloatField(default=0)
def save(self, *args, **kwargs):
# self.old money wont add here , as it will duplicate records .
self.sum_pocket = ( self.pocket_1000 * 1000 ) + (self.pocket_500 * 500) + (self.pocket_100 * 100) + \
(self.pocket_50 * 50) + (self.pocket_20 * 20 ) + (self.pocket_10 * 10) + \
(self.pocket_5 * 5) + (self.pocket_2 * 2 ) + self.pocket_1 + self.pocket_change
super(Pockets, self).save(*args, **kwargs)
def __unicode__(self):
return self.pocket_name
和相应的表格如下:
class PocketsTransactionForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PocketsTransactionForm, self).__init__(*args, **kwargs)
pocket_name = forms.CharField(widget=forms.TextInput(attrs={'type':'text','class':'pocketTotalAmount'}),max_length=300,required=False)
pocket_description = forms.CharField(widget=forms.TextInput(attrs={'type':'text','class':'pocketTotalAmount'}),max_length=500,label="Description",required=False)
class Meta:
model = Pockets
exclude = ['pocket_owner']
fields = ('pocket_name','pocket_description','pocket_1000','pocket_500','pocket_100','pocket_50','pocket_20','pocket_10','pocket_5','pocket_2','pocket_1','pocket_change')
现在在views.py中我使用2个表单和auto_id渲染它,所以我可以使用ID区分2个字段
form1 = PocketsTransactionForm(instance=p1,auto_id='pocketTransaction_p1_%s')
form2 = PocketsTransactionForm(instance=p2,auto_id='pocketTransaction_p2_%s')
return render_to_response('money/addPocketTransactionsShow.html', {'form1':form1,'form2':form2,}, context)
问题
如何通过request.POST从两个字段中获取pocket_name的值..请参阅下面的代码:
@login_required
def addPocketTransactionsShow(request):
context = RequestContext(request)
if request.method == 'POST':
print " pocket_name from form 1 is : ",request.POST.get("pocketTransaction_p1_pocket_name")
# how to get value in above line ... ids for pocket name 1 and 2 are pocketTransaction_p1_pocket_name and pocketTransaction_p2_pocket_name
任何建议?
答案 0 :(得分:0)
@login_required
def addPocketTransactionsShow(request):
context = RequestContext(request)
if request.method == 'POST':
form1 = PocketsTransactionForm(instance=p1,auto_id='pocketTransaction_p1_%s')
form2 = PocketsTransactionForm(instance=p2,auto_id='pocketTransaction_p2_%s')
if form1.is_valid():
pocket_name_1 = form1.cleaned_data['pocket_name']
if form2.is_valid():
pocket_name_2 = form2.cleaned_data['pocket_name']
# do what you want with pocket_name_1 and pocket_name_2