2014-02-12 3 views
0

저는 장고에서 완전히 새로 왔으며 처음으로 장고 양식을 사용하려고합니다. 나는 이걸 찾았지만 대답을 정확히 찾지 못했습니다. 기본적으로이 같은 전망이 있습니다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 
+0

이 필드를'PaymentForm'에 추가해야합니다 ... – Silwest

+0

질문을 업데이트하고 주석을 보내지 않고 양식 구조를 추가 할 수 있습니다. 'ModelForm'을 사용하기 때문에'PaymentModel'을 게시하는 것이 더 좋습니다. – FallenAngel

+0

제안한대로 스레드를 편집했습니다. –

답변

0

당신은 form.save()commit=False 키워드 인수를 사용할 수 있습니다

def pay(request):  
    if request.method == 'POST': 
     form = PaymentForm(request.POST) 
     if form.is_valid(): 
      # Will not save it to the database 
      myModel = form.save(commit=False) 

      # keep your business logic out of the view and put it on the form or model 
      # so it can be reused 
      myModel.checksum = form.calculate_checksum() 
      myModel.save() 
     else: 
      print form.errors 
    else: # The request is GET 
     form = PaymentForm() 

    return render_to_response('payment/payment.html', {'form':form}) 

Django form.save() documentation.

+0

불행히도 여전히 효과가 없습니다. 모델과 폼을 추가했습니다. 이유를 알 수 있습니까? –

+0

귀하의 문제가 무엇인지 분명하지 않습니다. 데이터베이스에 체크섬을 저장하고 싶습니까? –

+0

아니요 다른 데이터 (pid, sid, 금액 등)와 함께 체크섬을 압축하여이 필드가 필요한 외부 서버에 모두 보내려고합니다. –

관련 문제