设置表单字段值django

时间:2013-04-09 17:20:52

标签: django django-models django-views

我想设置表单值..我没有在表单中显示它,但想在我的视图中设置字段的值?

这是我的模型形式:

   class payment_detail(models.Model):
    status = (
        ('Paid','Paid'),
        ('Pending','Pending'),
    )
    id =             models.AutoField(primary_key=True)
    #ref_id =             models.CharField(max_length=32, default=_createId)

    #user = models.ForeignKey(User, editable = False)
    payment_type= models.ForeignKey(Payment_types,to_field = 'payment_types',  null=True, blank=True)

    job_post_id= models.ForeignKey(jobpost,to_field = 'job_id', null=True, blank=True)
    price= models.ForeignKey(package,to_field = 'amount',  null=True, blank=True)
    created_date =    models.DateField(("date"), default=datetime.date.today)
    payment_status =    models.CharField(max_length=255, choices=status,default='Pending')
    transaction_id =    models.CharField(max_length=255, null=True, blank=True)
    payment_date =    models.DateField(null=True, blank=True)
    email =    models.CharField(max_length=255, null=True)


    def __unicode__(self):
        #return self.user
        return unicode(self.id)
        #return self.ref_id
        return unicode(self.payment_type)
        return unicode(self.job_post_id)
        return unicode(self.price) 
        return unicode(self.created_date)
        return unicode(self.payment_status)
        return unicode(self.payment_date)
        return unicode(self.transaction_id)
        return unicode(self.email)


admin.site.register(payment_detail)

我的观点:

def payment(request):


    if "pk" in request.session:
     pk = request.session["pk"]
     Country = request.session["country"]


    price = package.objects.filter(item_type__exact='Job' ,country__country_name__exact=Country, number_of_items__exact='1')

    if request.method == 'POST':

        entity = payment_detail()
        form = jobpostForm_detail(request.POST, instance=entity) 

        if form.is_valid():  
          #form.fields["transaction_id"] = 100
          form.save()
          #message = EmailMessage('portal/pay_email.html', 'Madeeha ', to=[form.cleaned_data['email']])
          #message.send()

          return HttpResponseRedirect('/portal/pay/mail/')
    else:
        form = jobpostForm_detail(initial={'transaction_id': "US"})


    c = {}
    c.update(csrf(request))

    return render_to_response('portal/display.html',{
        'form':form,'price':price
    },context_instance=RequestContext(request))

就像我想设置job_location的值而不想在表单中显示它..

1 个答案:

答案 0 :(得分:1)

forms.py

//this is how you hide the field
class jobpostForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(jobpostForm, self).__init__(*args, **kwargs)
        self.fields['job_location'].widget = forms.HiddenInput()

    class Meta:
        model = jobpost

views.py

.........
if request.method == 'POST':
    entity = payment_detail(transaction_id="US") #change
    form = jobpostForm_detail(request.POST, instance=entity)

    if form.is_valid():
      #form.fields["transaction_id"] = 100
      form.save()
      #message = EmailMessage('portal/pay_email.html', 'Madeeha ', to=[form.cleaned_data['email']])
      #message.send()

      return HttpResponseRedirect('/portal/pay/mail/')
else:
    form = jobpostForm_detail()

 ..................