表单提交后数据未出现在数据库中

时间:2016-07-13 08:36:55

标签: python django python-2.7

我创建了一个试图进行简单注册的HTML表单。

问题是点击提交按钮后没有出现错误,但是当我收集数据库时,字段中的数据不存在。

signup.html

<form action="\polls\Registration" method="POST">
              <div class="form-group mb15">
                <input type="text" class="form-control" name="userName" placeholder="Enter Your Username" required>
              </div>
              <div class="form-group mb15">
                <input type="password" class="form-control" name="password" placeholder="Enter Your Password">
              </div>
              <div class="form-group mb15">
                <input type="text" class="form-control" name="fullName" placeholder="Enter Your Full Name">
              </div>
              <div class="form-group mb20">
                <label class="ckbox">
                  <input type="checkbox" name="checkbox">
                  <span>Accept terms and conditions</span>
                </label>
              </div>
              <div class="form-group">
                <button class="btn btn-success btn-quirk btn-block">Create Account</button>
                <br>
                <a href="/polls/signin" class="btn btn-default btn-quirk btn-stroke btn-stroke-thin btn-block btn-sign">Already a member? Sign In Now!</a>
              </div>
            </form>

forms.py

class RegistrationForm(forms.Form):
    userName= forms.CharField(label='Username',max_length=100)
    password = forms.CharField(label='Password', max_length=100)
    fullName= forms.CharField(label='Full Name', max_length=100)

myview.py

def reg(request):

    if request.method == 'POST':
         the request:
        form = forms.RegistrationForm(request.POST)

        if form.is_valid():

            return HttpResponseRedirect('/polls/signin')

    else:
        form = forms.RegistrationForm()

    return render(request, 'signup.html', {'form': form})

urls.py

urlpatterns = [        
    url(r'^Registration', myview.reg,name='Registration'),
]

1 个答案:

答案 0 :(得分:0)

[解决] 我所做的是我创建了一个模型来处理表单处理。我还使用了modelForm而不是表单。对于有相同问题的任何人,请检查this