所以在我的模型中。我有 blank=True 以便输入的表格可以是空白的。但是当用户提交表单时,它会在数据库中写入一个空白项目,而这并不是我真正希望它会做的事情。是否有其他选项来控制空白表单的处理方式?
编辑:最后,我希望表单将所有带有字符的字段添加到数据库中,并省略所有没有输入字符的字段。
模型.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))