django - 用户注册:创建用户帐户 Django 2.1.5
问题描述
我在创建新帐户然后进行身份验证时遇到了一些问题。我在(用户名、密码)中输入所有凭据,然后选择“提交”,它成功地将我重定向回“帐户成功创建页面”。但是我在数据库和员工帐户中找不到新的用户帐户。
这是我的意见文件
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()
方法会将用户保存到数据库中。
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})
推荐阅读
- python-3.x - Azure ML 错误:部署模型时必须提供 InferenceConfig,其中 model_framework 设置为 AutoML
- java - 内部类没有出现在 jar 中
- python - 在 Spyder 中打开 .GRIB 文件时出现 ECCODES 错误
- kibana - Kibana:如何聚合所有 UUID
- datatable - 在 Datatable 中的单个列中搜索
- android - 在android中的位图上绘制文本
- reactjs - 如何在 useEffect 中更新状态对象
- azure-devops - 在构建变量中检测失败构建的重新运行
- jquery - 将 jQuery 选择器应用于容器及其后代
- css - 如何在另一个 div 上显示带有孩子的 div