任何帮助将不胜感激,我对 django 和开发 Web 应用程序相当陌生,但到目前为止,我已经能够基本上暴力破解所有内容或自行解决;然而,这让我很难过。
出于某种原因,我的代码似乎试图从我的字段中提交整个 html,而不仅仅是它包含的数据,我不知道为什么。
错误消息调试显示:
POST Variable Value
submitter u'ccroffor'
csrfmiddlewaretoken u'P8s3aL4bkyzZJWHPitQVEIFS6Lxt3s6e'
sac_id u'892064'
sr_number u'3-3016786463'
但是查询显示了这一点:
query
'INSERT INTO `autoed_submission` (`sr_number`, `sac_id`, `submission_time`, `completed_time`, `submitter`, `submission_helpful`, `submission_link`) VALUES (\'<textarea cols=\\"40\\" id=\\"id_sr_number\\" name=\\"sr_number\\" placeholder=\\"ex... 3-3010581928\\" rows=\\"1\\">\\r\\n3-3016786463</textarea>\', \'<textarea cols=\\"40\\" id=\\"id_sac_id\\" name=\\"sac_id\\" placeholder=\\"ex... 890046, 890052, ...\\" rows=\\"1\\">\\r\\n892064</textarea>\', \'2013-04-20\', NULL, \'<textarea cols=\\"40\\" id=\\"id_submitter\\" name=\\"submitter\\" placeholder=\\"ex... ccroffor\\" rows=\\"1\\">\\r\\nccroffor</textarea>\', \'U\', \'undefined\')'
视图.py
def mySubView(request):
if request.method == 'POST': #checking request
form = AutoEdForm(request.POST)
if form.is_valid(): #validating form
newSubmission = Submission(sr_number = form['sr_number'], sac_id = form['sac_id'], submitter = form['submitter'], submission_time = datetime.now(tzz)) #formatting form data to db model
newSubmission.save() #saving data to db
return HttpResponseRedirect('/thanks/') #redirecting to thanks page
else:
form = AutoEdForm()
return render_to_response('submission.html', {'form':form,}, context_instance=RequestContext(request))
表格.py
class AutoEdForm(forms.Form):
sr_number = forms.CharField(
max_length=100,
label=u'SR Number',
widget=forms.Textarea(
attrs={
'placeholder': 'ex... 3-3010581928',
'rows': '1',
})
)
sac_id = forms.CharField(
max_length=100,
label=u'Sac ID',
widget=forms.Textarea(
attrs={
'placeholder': 'ex... 890046, 890052, ...',
'rows': '1',
})
)
submitter = forms.CharField(
max_length=100,
label=u'nai-corp Username',
widget=forms.Textarea(
attrs={
'placeholder': 'ex... ccroffor',
'rows': '1',
})
)
def clean(self):
cleaned_data = super(AutoEdForm, self).clean()
return cleaned_data
相关的html
{% load bootstrap_toolkit %}
<div class="container well">
<form class="form-horizontal" action="" method="post">
{% csrf_token %}
{% for field in form %}
{% include "bootstrap_toolkit/field_horizontal.html" %}
{% endfor %}
<input type="submit" value="Submit" class="btn btn-primary" style="margin-left:180px">
</form>
</div> <!-- /container -->
模型.py
class Submission(models.Model):
ED_VALUE = (
('A', 'Averted'),
('N', 'Not Averted'),
('U', 'Unanswered'),
)
sr_number = models.CharField(max_length=100)
sac_id = models.CharField(max_length=100)
submission_time = models.DateField(null=True)
completed_time = models.DateField(null=True)
submitter = models.CharField(max_length=100)
submission_helpful = models.CharField(max_length=100, choices=ED_VALUE, blank=False, default="U")
submission_link = models.CharField(max_length=100, blank=False,default="undefined")