Django Form Form Submit and verify the Form.1 method in Django to verify the correctness of the form submission of the form

2023-03-18  

Article Source:https://www.cnblogs.com/momo8238/p/7493305.html

1. An error will be reported when submitted

2. You need to note a sentence in setting and turn off the cross -site request for inspection.

3. After the annotation is lost, it will not be wrong in theory. But I’m still stuck.

4. Fix the error by looking for methods online.

Reason: Form ACTION field does not end with ‘/’, modify the action to the action = “/a/b/” to repair it. Code the action field as follows:

Here you need to explain that in Django, add one by default to the back of the browser/. For example: 127.0.0.1: 8000/login, it will be corrected as:

127.0.0.1:8000/login/.

So if/if/in the URL, you must also add//. Either the two are not added, otherwise an error will be reported.

url (R ‘^login/’, views.login) must match the action = ‘/login/’

5. When submitted at this time, it will not be wrong.

6. Restart the company. When there are requests, they go to urls.py to find the corresponding relationship.

The background should obtain different methods according to different user submission methods (get/post).

Generally speaking, the form is obtained by get, and the user name and password are submitted in a post.

Request contains all the information sent by the client user.

If [Post], the form data will be sent out in the request body.
If it is [Get], the form data will be added to the query string, submitted to the server in the form of query string. eg: www.baidu.com?nid=1&Name=2
Suggestion: Forms are usually better submitted in POST, so that it can not destroy the URL. Moreover, the URL also has a length limit.

1

2

3

user=request.POST['user']

pwd = request.POST['pwd']

print(user,pwd)

Test the background to obtain the username and password. Request.post () is similar to a dictionary.

In order to prevent errors, you can modify the above program:

dict.get (key, default = none), if it is worth it, obtain; if not, use the default value. With the default value, it will not report an error.

7. At this point, you can restart the server side, then submit it with root, 123 as the user name and password, and successfully jump to the Baidu page.

jump:

8. After verification, if the user name and password are wrong, return to the client.

8.1: Add 1 label to Login.html

8.2 Add the third parameter to render in the view.py,

Return Render (request, 'login.html', {'error_msg': 'user name password does not match'}) 

 Render will find the login.html directory first, then open the login.html file to put it in memory, then find the special character of error_msg, and then replace the Error_msg string to the "user name password that does not match 'string.

Run results: So far everything is over.

source

Random Posts

java to list the string array to list

Oracle View Object Space usage SHOWSPACE

UNI-APP development mini program packaging API interface request

lightoj 1074 [SPFA Curricope] Keyboarder

Animation simulatory carton blasting (implemented by AnimationSet) yang