我正在使用 Python-Django 和 MySQL 构建一个网站。
我正在尝试使用 Jquery.Validate 向我的表单添加验证,它工作正常。我正在努力让用户可以保存表单并提交表单。现在我无法提交或保存到数据库,除非我填写所有字段:
Django 显示此错误:[u'This field is required.']
这是因为我设置的 django 模型吗?或者有没有办法让我在提交表单时不需要这些字段,以便数据库将这些字段填充为空或空或 w/e?
模型.py
class ProjectRequest(Model):
reqType = CharField("Request Type", max_length = MAX_CHAR_LENGTH)
reqID = AutoField("Request ID", primary_key = True)
class ProjectRequestForm(ModelForm):
class Meta:
model = ProjectRequest
视图.py
def requestProject(request):
form = ProjectRequestForm()
if request.method == 'POST':
form = ProjectRequestForm(request.POST)
print "%s" % repr(form.errors)
if form.is_valid():
reqType = request.POST.get('reqType')
//reqID is autoincremented in database
reqProject = ProjectRequest(
reqType = reqType,
)
if request.POST.get('reqStatus') == 'Submit':
createTicket(request)
reqProject.save()
return HttpResponseRedirect('/')
else:
form = ProjectRequestForm()
response = render_to_response('reqProject.html', {'form': form}, context_instance = RequestContext(request))
return response
另外为了验证,这是我的 javascript:
$("#submitRequest").click(function(){
$("#requestForm").validate();
$('#reqProjectModal').modal('show');
});
HTML:
<form class="form-horizontal well" id="requestForm" action="" method="post">
{% csrf_token %}
<input type="hidden" name = "reqType" value ="Project">
<div class="control-group">
<label class="control-label" for="projCodeName">Project Code Name</label>
<div class="controls">
<input class="required span4" minlength="4" id="projCodeName" type="text" name="projCodeName" placeholder="What are you calling your project?"/>
</div>
</div>
<input class="btn btn-primary" type="submit" name="reqStatus" value="Saved Draft">
<input class="btn" type="reset">
<input class="btn btn-primary" type="submit" name="reqStatus" value="Submit" id="submitRequest">
</form>
<div class="modal hide fade" id="reqProjectModal" aria-hidden="true">
<div class="modal-header">
<h3 align="center">Request is being Submitted</h3>
</div>
我只想在单击 submitRequest 时验证表单,但如果单击 Saved Draft,我也无法提交任何内容。对于模态,我在提交时会出现一个模态旋转圆圈,因为 submitRequest 还会生成一个需要时间的票证。如果表单有效并且 POST 正在通过,我只希望显示模态圈。有什么办法也可以做到这一点?!