2012-04-05 17 views
0

私のモデルではそうです。私はblank = Trueを入力して、入力されたフォームを空白にすることができます。ユーザーがフォームを送信すると、空のアイテムがデータベースに書き込まれます。これは実際には私が望むものではありません。空白のフォームがどのように扱われるかを制御する他のオプションがありますか?Djangoのフォーム提出トラブルシューティング/データベース管理?

EDIT:最終的に、フォームにデータベース内の文字を含むすべてのフィールドを追加し、文字が入力されていないフィールドをすべて省略するようにします。ここ

models.py

class Cashtexts(models.Model): 
    cashcode = models.CharField(max_length=100, blank=True) #change me to a website filter 
    superPoints_Link = models.CharField(max_length=100, blank=True)#chance to "superPoints _Username" 
    varolo_username = models.CharField(max_length=100, blank=True) 
    swagbucks_username = models.CharField(max_length=100, blank=True) 
    neobux_username = models.CharField(max_length=100, blank=True) 
    topline_id = models.CharField(max_length=100, blank=True) 
    Paidviewpoint_id = models.CharField(max_length=100, blank=True) 
    cashcrate_id = models.CharField(max_length=100, blank=True) 

そして、私は 文字を含むすべてのフィールドを入力するフォームの希望とフォームの空の入力を省略するためにビュー

def submit_win(request): 
    if request.method == 'POST': 
     if Cashtexts.objects.filter(cashcode=request.POST['cashcode']).exists() or Cashtexts.objects.filter(superPoints_Link=request.POST['superPoints_Link']).exists() or Cashtexts.objects.filter(varolo_username= request.POST['varolo_username']).exists() or Cashtexts.objects.filter(swagbucks_username = request.POST['swagbucks_username']).exists() or Cashtexts.objects.filter(neobux_username = request.POST['neobux_username']).exists() or Cashtexts.objects.filter(superPoints_Link=request.POST['topline_id']).exists() or Cashtexts.objects.filter(superPoints_Link=request.POST['Paidviewpoint_id']).exists() or Cashtexts.objects.filter(superPoints_Link=request.POST['cashcrate_id']).exists() == False: 
      form = CashtextsForm(request.POST) 
      if form.is_valid(): 
       c={} 
       c.update(csrf(request)) 
       form.save() 
       return render_to_response('submitted_page.html',c) 
     else: #Error message reading wither you didnt insert codes or you enter the same code twice 
      error = 'you have already entered that code' 
      ref_create = CashtextsForm() 
      return render_to_response('submit.html', {'ref_create': ref_create,'error':error}) 
    else: #displays the page when the user asks to go to the submit page 
     ref_create = CashtextsForm() 
     return render_to_response('submit.html', {'ref_create': ref_create}, RequestContext(request)) 
+0

は何が起こることをしたいですか? – thebjorn

+0

私はフォームが文字を含むすべてのフィールドを入力し、フォームが空の文字列を入力するのを省略することを望みます。私は複数の送信ボタンでこれを行うことができますが、それは2つ以上のフィールドの痛みになります。 – city

+0

コードを追加していただきありがとうございます。私はあなたの質問を誤解しているように見えるので、私は本当に関係がないので私の答えを削除しました。あなたが解決策を見つけることを願っています。一般的なヒントとして、モデルをコードのタイプ(現金コード、スーパーポイントなど)を記述するフィールドと値を格納するフィールドの2つのフィールドに変更することを検討してください。コードを簡素化することができます。 – Alasdair

答えて

0

です私が実現している文字列 複数の送信ボタンでこれを行うことはできますが、2つまたは3つ以上のフィールドに対しては の痛みになります。

空の文字列を必要としない場合は、フィールドが空の場合は[なし]を選択すると仮定できます。 最初に、モデル(およびデータベース)が必要です。そうしないとモデルが動作しません。

は、私は次のように動作するはずと信じて:(あなたが本当にこれをしたいと思う理由は、私はわからないが)

class Cashtexts(models.Model): 
    cashcode = models.CharField(max_length=100, null=True, blank=True, default=None)