我在创建新帐户然后进行身份验证时遇到了一些问题。我在(用户名,密码)中输入所有凭据,然后选择“提交”,并成功将我重定向回“帐户成功创建的页面”。但是,我无法在数据库和员工帐户中找到新用户帐户。
这是我的观点文件
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login
# Create your views here.
def index(request):
return render(request, 'user_example/index.html')
def register(request):
if request.method=='POST':
form=UserCreationForm(request.POST)
if form.is_valid():
form.save()
username=form.cleaned_data.get('username')
raw_password=form.cleaned_data.get('password1')
'''user=User.objects.create_user(username=username, password=raw_password)
user.save()'''
user=authenticate(username=username, password=raw_password)
user.save()
#if user1 is not None:
login(request, user)
return redirect('success')
else:
form=UserCreationForm()
#context={'form': form}
return render(request, 'registration/register.html', {'form': form}) #as
def success(request):
return render(request, 'user_example/success.html')
这是我的成功模板:
<!DOCTYPE html>
<html>
<head>
<title>Successfull!</title>
</head>
<body>
{% if user.is_authenticated %}
<h1>Congratulations {{ user.username }}!! you have successfully created an account.</h1>
{% endif %}
<p>You can go back to the<a href="{% url 'index' %}"> homepage
</a>now.</p>
</body>
</html>
这是我的注册模板:
{% block title %}Register{% endblock %}
{% block register_active %}active{% endblock %}
{% block body %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h3>Create an Account</h3>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form class="form-horizontal" role="form" action="{% url 'success' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'user_example/form-template.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
<br>
<p><strong>-- OR --</strong></p>
<div class="panel-footer">
{% load socialaccount %}
<a href="{% provider_login_url 'github' %}">SignUp with Github</a>
}
</div>
<div class="panel-footer">
Already have an account? <a href="">Click here</a> to log in.
</div>
</div>
</div>
</div>
</div>
{% endblock %}
请帮忙,我在这里做错了什么?
在你给定的代码中,你已经注释掉了必要的部分:(删除那个评论,就是这样:)
除此之外,user.save()
语句是无关紧要的,因为create_user()
方法将用户保存到DB。
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
# uncomment below line
user = User.objects.create_user(username=username, password=raw_password)
user.save() # remove this line
user = authenticate(username=username, password=raw_password)
user.save()
# if user1 is not None:
login(request, user)
return redirect('success')
else:
form = UserCreationForm()
return render(request, 'registration/register.html', {'form': form})