Python Django 注册并使用确认邮件登录
Django默认提供了一个认证系统配置。用户对象是认证系统的核心。今天我们将实现Django的认证系统。
需要的模块:
- django : django安装
- crispy_forms :
pip install --upgrade django-crispy-forms
基本设置:
通过以下命令启动一个项目-
django-admin startproject project
将目录改为项目-
cd project
启动服务器- 通过在终端键入以下命令启动服务器 –
python manage.py runserver
要检查服务器是否在运行,请进入一个网页浏览器,输入http://127.0.0.1:8000/_ 作为URL。
现在,通过按以下步骤停止服务器
ctrl-c
让我们现在创建一个名为 “用户 “的应用程序。
python manage.py startapp user
通过操作进入用户/文件夹。cd user并创建一个文件夹templates,里面有index.html, login.html, Email.html, register.html文件。
用一个文本编辑器打开项目文件夹。目录结构应该是这样的。
现在在settings.py中的todo_site中添加 “user “应用程序和 “crispy_form”,并添加
CRISPY_TEMPLATE_PACK = 'bootstrap4'
在settings.py的最后
在setting.py中配置邮件设置。
把你的电子邮件和密码放在这里。
编辑 urls.py 项目中的文件:
from django.contrib import admin
from django.urls import path, include
from user import views as user_view
from django.contrib.auth import views as auth
urlpatterns = [
path('admin/', admin.site.urls),
##### user related path##########################
path('', include('user.urls')),
path('login/', user_view.Login, name ='login'),
path('logout/', auth.LogoutView.as_view(template_name ='user/index.html'), name ='logout'),
path('register/', user_view.register, name ='register'),
]
在用户中编辑urls.py :
from django.urls import path, include
from django.conf import settings
from . import views
from django.conf.urls.static import static
urlpatterns = [
path('', views.index, name ='index'),
]
在用户中编辑 views.py:
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm
from .forms import UserRegisterForm
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Context
#################### index#######################################
def index(request):
return render(request, 'user/index.html', {'title':'index'})
########### register here #####################################
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
email = form.cleaned_data.get('email')
######################### mail system ####################################
htmly = get_template('user/Email.html')
d = { 'username': username }
subject, from_email, to = 'welcome', 'your_email@gmail.com', email
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
##################################################################
messages.success(request, f'Your account has been created ! You are now able to log in')
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'user/register.html', {'form': form, 'title':'register here'})
################ login forms###################################################
def Login(request):
if request.method == 'POST':
# AuthenticationForm_can_also_be_used__
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username = username, password = password)
if user is not None:
form = login(request, user)
messages.success(request, f' welcome {username} !!')
return redirect('index')
else:
messages.info(request, f'account done not exit plz sign in')
form = AuthenticationForm()
return render(request, 'user/login.html', {'form':form, 'title':'log in'})
在这里配置你的电子邮件。
现在在用户中创建一个 forms.py :
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
phone_no = forms.CharField(max_length = 20)
first_name = forms.CharField(max_length = 20)
last_name = forms.CharField(max_length = 20)
class Meta:
model = User
fields = ['username', 'email', 'phone_no', 'password1', 'password2']
浏览到 templates/user/ 并编辑文件:
- 链接到index.html文件
- 链接到Email.html
- 链接到login.html
- 链接到register.html
制作迁移程序并进行迁移。
python manage.py makemigrations
python manage.py migrate
现在你可以运行服务器来查看你的应用程序。
python manage.py runserver