首页新闻招聘找找看知识库
  • 浏览:61 2017-04-05 11:09 来自 大博

    目标:使用django forms表单实现两个参数的求和 实现代码: forms.py from django import forms

    class AddForm(forms.Form): a=forms.IntegerField() b=forms.IntegerField()

    views.py from django.shortcuts import render from django.http import HttpResponse

    from .forms import AddForm

    def TestForm(request): if request.method=='POST': form=AddForm(request.POST)

        if form.is_valid():        a=form.cleaned_data['a']        b=form.cleaned_data['b']        return HttpResponse(str(int(a)+int(b)))    else:        form=AddForm()    return render(request,'index.html',{'form':form})

    index.html <form method='post'> {% csrf_token %} {{form}} <input type="submit" value="提交"> </form>

    urls.py from django.conf.urls import url from django.contrib import admin from tools.views import TestForm

    urlpatterns =[ url(r'^admin/', admin.site.urls), url(r'^form/',TestForm,name='testform'), ]

    希望大神能指导如何分析该报错信息,并且根据报错信息到源码去找到错误处?

登录后才能评论,请先登录注册