testform.html
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>测试form</title>
</head>
<body>
<h3>录入与显示</h3>
<div class = 'load_and_display'>
<form name = 'load_and_display' action ='/' method='POST'>
{% csrf_token %}
{{form.as_p}}
<input type='submit' value='录入'>
</form>
</div>
<p>
ID: {{ cell_id }}<br>
容量: {{ cell_Ah }}<br>
电压: {{ cell_Volt }}<br>
反馈 {{message}}<br>
</p>
<br>
</body>
</html>
views.py
def testform(request):
cell_id = ''
cell_Ah = ''
cell_Volt = ''
if request.method == 'POST':
form = forms.FormTest1(request.POST)
if form.is_valid():
cell_id = form.cleaned_data['cell_id']
cell_Ah = form.cleaned_data['cell_Ah']
cell_Volt = form.cleaned_data['cell_Volt']
message = '输入信息正确'
else:
message = '输入信息错误'
else:
form = forms.FormTest1()
# template = get_template('testform.html')
# html = template.render(locals())
# return HttpResponse(html)
return render(request, 'testform.html',locals())
forms.py
from django import forms
from operateMysql import models
class FormTest1(forms.Form):
cell_id = forms.CharField(label='扫码ID', max_length = 50, initial = '')
cell_Ah = forms.CharField(label='容量/Ah', max_length = 50, initial = '')
cell_Volt = forms.CharField(label='电压/mV', max_length = 50, initial = '')