测试django form库

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 = '')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值