Define forms.py under ./apps/users to verify the data that needs to be verified
Perform simple verification on the data: variable name (passed parameter name) = form.CharField (simple verification type = standard value, error message dictionary)
User name, password, confirm password, mobile phone to verify the maximum length, minimum length, and whether it is required
The verification of user name and mobile phone number can be verified by ajax on the front end, and it will not be processed here
Graphic verification code: no verification is required. After the graphic verification code is correct, the SMS verification can be sent, otherwise the SMS verification code cannot be sent
SMS verification code: This film only has the length and required fields for simple verification, and there is a special verification algorithm to judge the correctness of the SMS verification code
Define clean(self) function, extend data verification
First call the clean(self) verification function of the base class itself: cleaned_data = super().clean()
Then judge whether the two passwords are the same,
Receive and verify parameters: instantiate the verification processing class
Judging the verification result
If the verification is correct:
Take out user name, password, mobile phone number
Save registration information to the database: use the user model class method provided by Django
State keep (not write temporarily)
Redirect back to the homepage
If the check is wrong:
Take out the check error message and generate the error message in the format to be returned
Return to the registration page (or home page)
from django.shortcuts import render
from django.views import View
from.forms import RegisterForm
# from .models import Userfrom users.models import User
from django import http
classRegisterView(View):"""用户注册"""defget(self,request):"""提供用户注册页面"""return render(request,'register.html')defpost(self,request):"""提供用户注册逻辑"""# 校验参数
register_form = RegisterForm(request.POST)if register_form.is_valid():
username = register_form.cleaned_data.get('username')
password = register_form.cleaned_data.get('password')
mobile = register_form.cleaned_data.get('mobile')# 保存到数据库中try:
User.objects.create_user(username=username, password=password, mobile=mobile)except Exception as e:return render(request,'register.html',{
'register_errmsg':'注册失败'})# 状态保持# 响应结果return http.HttpResponse('注册成功, 重定向到首页')# return redirect(reverse('contents:index'))else:print(register_form.errors.get_json_data())
context ={
'forms_errors': register_form.errors
}return render(request,'register.html', context=context)
3.4 here an exception handling
3.4.1 Description of error message:
If the following error message appears
3.4.2 Solution:
Need to check the guide package path
将 from .models import User 改为 from users.models import User
At the same time change lgshop to Scoures Root
3.4.3 Solution Two:
./lgshop/urls.py
3.5 Save registration data
Here, use the create_user() method provided by the Django authentication system user model class to create a new user.
Here the create_user() method encapsulates the set_password() method to encrypt the password.
3.6 Respond to the registration result
Registration is successful, redirect to the home page (the premise is to define the home page, the steps are as follows)
Create a homepage content application
Write index.html
Write view method
Register the contents application
3.7 State maintenance
If the requirement is that the user login is successful after the registration is successful, then the state can be maintained after the registration is successful.
If the requirement is that the successful registration does not mean that the user login is successful, then there is no need to maintain the state after the successful registration at this time
State maintenance: Write the unique identification information (such as user ID) of the authenticated user into the cookie of the current browser and the session of the server.
3.7.1 Django's login() method (state retention)
The Django user authentication system provides the login() method: encapsulates the operation of writing to the session, helps us quickly log in to a user, and achieve state retention.
login () Location: django.contrib.auth. the init .py file.
3.7.2 State keeps the location of session data storage
The session is stored in the No. 1 library of the Redis database
Three, user repeated registration verification (backend)
1. Logical analysis of user name duplication registration
When the user registers, after all the information is input and submitted, the user will be told that the user has been registered. This kind of user experience is not friendly
Therefore, after the user name input box loses focus and the simple verification is correct, the user repeats the registration verification request to the background. If the repeated verification prompts that the user name has been registered and needs to be replaced, if it is not registered, nothing will be prompted
This operation can use ajax for asynchronous operation to improve user experience
2. Design and definition of user name duplicate registration interface
2.1 Request method
Options
Program
Request method
GET
Request address
/usernames/(?P[a-zA-Z0-9_-]{5,20})/count/
2.2 Request parameters: path parameters
parameter name
Types of
Must pass
Description
username
string
Yes
username
2.3 Response result: JSON
Response result
Response content
code
status code
errmsg
Error message
count
Record the number of the username
3. Back-end implementation
The user name repeated registration is not html, just return a json.
3.1 Define the view method
User name duplicate registration verification should not be implemented in the user registration business, and the interface is also different, so define another UsernameCountView class in users.view.py
The UsernameCountView class only needs a get method
Just need to count how many usernames in the database, there should be only one or none
So, after fetching the data, just return directly
Note: The user base class used in the data model is the user class of django, pay attention to the method of use