Python 视图SugarNoter.views.Index没有';t返回HttpResponse对象。它没有返回任何结果
每当新用户要注册时,在此之后,即使返回HttpResponse,我也会收到此错误: 视图SugarNoter.views.Index未返回HttpResponse对象。它没有返回任何结果 views.py:Python 视图SugarNoter.views.Index没有';t返回HttpResponse对象。它没有返回任何结果,python,django,Python,Django,每当新用户要注册时,在此之后,即使返回HttpResponse,我也会收到此错误: 视图SugarNoter.views.Index未返回HttpResponse对象。它没有返回任何结果 views.py: def Index(request): if request.method=='POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save
def Index(request):
if request.method=='POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
passwd = form.clean_password2()
user = auth.authenticate(username=username, password=passwd)
auth.login(request, user)
return redirect(f'users/{str(request.user)}')
else:
context = {
'form':UserCreationForm()
}
return render(request, 'index.html', context)
html文件
{% load crispy_forms_tags %}
<div class="container">
<div class="col-sm-6 my-5 mx-auto">
<div class="card align-items-center justify-content-center">
<div class="card-body">
<form method="POST">
{% csrf_token %}
{{ form | crispy }}
<button type="button" class="btn btn-danger" onclick='window.location="login"'>Login</button>
<button type="submit" class="btn btn-success">Sign Up</button>
</form>
</div>
</div>
</div>
</div>
{%load crispy_forms_tags%}
{%csrf_令牌%}
{{form | crispy}}
登录
注册
我该怎么办?您没有处理您的
表单.is\u valid()
为false时的情况,
在这种情况下,您没有返回语句。
所以你也需要为其他条件返回一些东西。。。
或者将重定向语句移出if条件
def Index(request):
if request.method=='POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
passwd = form.clean_password2()
user = auth.authenticate(username=username, password=passwd)
auth.login(request, user)
return redirect(f'users/{str(request.user)}')
else:
context = {
'form':UserCreationForm()
}
return render(request, 'index.html', context)
希望这有帮助 谢谢大家!!谢谢你的帮助