如何给一个新项目添加登陆、注册、改密码、重置密码、邮件找回密码等功能?
一、把下面两个红框里的html拷贝到新项目的对应位置,一定要位置一样(具体html文件,在我的博客园文件栏目里保存),
html文件里,出现类似{% url "shop:login" %}的地方,切记,shop要换成实际项目中的应用名称。
二、settings如下设置。
shop:product_list表示登录成功后,跳转到哪个path(‘product_list/’)url地址。
shop:login表示登陆用哪个path('login/')url地址。
LOGIN_REDIRECT_URL = 'shop:product_list' LOGOUT_REDIRECT_URL = 'shop:product_list' LOGIN_URL = 'shop:login' LOGOUT_URL = 'shop:logout' EMAIL_HOST = 'smtp.163.com' EMAIL_HOST_USER = 'xxxx@163.com' # 填写你的邮件地址 EMAIL_HOST_PASSWORD = 'xxxxxxxx' # 邮箱的smtp授权码 EMAIL_PORT = 25 EMAIL_USE_TLS = True DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
三、按照下方代码设置base.html
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}{% endblock %}</title> <link href="{% static "css/base.css" %}" rel="stylesheet"> </head> <body> <div id="header"> <a href="/" class="logo">My shop</a> </div> {% if request.user.is_authenticated %} <div id="subheader" style="margin-left: 20px"> <div class="cart"> Your cart is empty. </div> </div> {% endif %} <div style="margin-right: 20px"> {% if request.user.is_authenticated %} Hello {{ request.user.first_name }},<a href="{% url "shop:password_change" %}">修改密码</a> <a href="{% url "shop:logout" %}">Logout</a> {% else %} <a href="{% url "shop:login" %}">Log-in</a> {% endif %} </div> <div id="content"> {% block content%} {% endblock %} </div> </body> </html>
四、在下方的forms.py文件里,粘贴下面的代码:
from django import forms from django.contrib.auth.models import User class LoginForm(forms.Form): username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput) class UserRegistrationForm(forms.ModelForm): password = forms.CharField(label='Password', widget=forms.PasswordInput) password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput) class Meta: model = User fields = ('username', 'first_name', 'email') def clean_password2(self): cd = self.cleaned_data if cd['password'] != cd['password2']: raise forms.ValidationError('Password don\'t match.') return cd['password2']
五、在shop里面的urls.py文件,粘贴下面的代码:
from django.urls import path from django.contrib.auth import views as auth_views from . import views app_name = 'shop' urlpatterns = [ path('login/', auth_views.LoginView.as_view(), name='login'), path('logout/', auth_views.LogoutView.as_view(), name='logout'), path('', views.product_list, name='product_list'), # change password urls path('password_change/', auth_views.PasswordChangeView.as_view( template_name='shop/password_change_form.html', success_url="/password_change/done/", ), name='password_change'), path('password_change/done/', auth_views.PasswordChangeDoneView.as_view( template_name='shop/password_change_done.html' ), name='password_change_done'), # reset password urls path('password-reset/', auth_views.PasswordResetView.as_view( template_name="shop/password_reset_form.html", email_template_name="shop/password_reset_email.html", subject_template_name="shop/password_reset_subject.txt", success_url="/password-reset-done/", ), name='password_reset'), path('password-reset-done/', auth_views.PasswordResetDoneView.as_view( template_name="shop/password_reset_done.html" ), name='password_reset_done'), path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view( template_name="shop/password_reset_confirm.html", success_url="/password-reset-complete/", ), name='password_reset_confirm'), path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view( template_name="shop/password_reset_complete.html" ), name='password_reset_complete'), path('register/', views.register, name='register'), ]
在myshop项目里的urls.py粘贴下面代码:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('shop.urls', namespace='shop')), ]
六、在下方views.py文件里,粘贴下面的代码:
from django.shortcuts import render, get_object_or_404 from .models import Category, Product from cart.forms import CartAddProductForm from django.contrib.auth.decorators import login_required from .forms import UserRegistrationForm def register(request): if request.method == 'POST': user_form = UserRegistrationForm(request.POST) if user_form.is_valid(): # 创建一个新的用户对象,但要避免保存它 new_user = user_form.save(commit=False) # 设置选择的密码 new_user.set_password(user_form.cleaned_data['password']) # 保存用户对象 new_user.save() return render(request, 'shop/register_done.html', {'new_user': new_user}) else: user_form = UserRegistrationForm() return render(request, 'shop/register.html', {'user_form': user_form}) @login_required def product_list(request, category_slug=None): # ......... @login_required def product_detail(request, id, slug): # ........