CSRF验证失败。请求在django中止

时间:2016-10-13 10:52:26

标签: django django-forms django-templates django-views

我正在User_Auth数据库中注册用户,但我收到了CSRF_tokens错误... 在内置的Django用户数据库中提交的数据

register.html

    <div class="container-fluid" style="background-color: #f2f2f2;">
    <div class="row">
        <div class="col-lg-3"></div>
            <div class="col-lg-6 " align="center" style="margin:50px;">
                <h3>Register for an Account</h3>
                    <form class="form-horizontal" action="" method="post">
                        {% csrf_token %}
                        {{form.errors}}
                        <table>
                            <tr><td>Username:<input type="text"  value=""name="username"></td></tr><br>
                            <tr><td>Password: <input type="password" value=""name="password"></td></tr>
                            <!--Superuser:<select>-->
                                <!--<option value="0" name="is_superuser">0</option>-->
                                <!--<option value="1" name="is_superuser">1</option>-->
                            <!--</select>-->
                        </table>
                        <div align="center"><button type="submit" class="btn btn-success pull-right">Submit</button></div>
                    </form>
            </div>
        <div class="col-lg-3"></div>
    </div>
</div>

views.py

def register(request):
if request.method=='POST':
    form = UserForm(request.POST)
    if form.is_valid():
        user = User.objects.create(
            username = form.cleaned_data['username'],
            password = form.cleaned_data['password'],
        )
        user.save()
        return HttpResponseRedirect('signin')
else:
    form=UserForm()
return render(request, 'customer/register.html', {'form': form})

这是我的 forms.py

class UserForm(forms.ModelForm):
class Meta:
    model = User
    fields = ['username', 'email', 'password']

1 个答案:

答案 0 :(得分:0)

更改

{{ csrf_input }}

{% csrf_token %}