Django中的表单提交故障排除/数据库管理?

时间:2012-04-05 18:40:55

标签: python django forms

所以在我的模型中。我有空白= True,因此输入的表格可以为空白。但是当用户提交表单时,它会将一个空白项写入数据库,这并不是我希望它能做到的。是否有其他选项来控制如何处理空白表单?

编辑:最后我想让表单将包含字符的所有字段添加到数据库中,并省略所有没有输入字符的字段。

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))

1 个答案:

答案 0 :(得分:0)

  

我希望表单输入包含的所有字段   字符和表单省略输入空字符串我意识到   我可以用多个提交按钮执行此操作,但这会成为一个   超过2或3个领域的疼痛。

如果你不想要空字符串,那么我只能假设你想要空的字段为None。 首先,您需要Model(以及数据库)来允许,否则它将无法工作。

我相信以下内容应该有效:(虽然我不确定你为什么真的想要这个)

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