django - Editing Students

Adding Editor Links to Student List Page Write routes so that the path corresponds to the view function skill, quickly create view functions Editing ...
Adding Editor Links to Student List Page

Write routes so that the path corresponds to the view function skill, quickly create view functions

Editing pages
<form action="" method="post"> <p>Full name:<input type="text" name="name"></p> <p>Gender: <select name="gender" id=""> <option value="male">male</option> <option value="female">female</option> <option value="neutral">neutral</option> <option value="secrecy">secrecy</option> </select> </p> <input type="submit" value="Confirmation of modifications"> </form>

Next, click the Edit button to see the edit page.

Judgment in View

If it is a get request, render the edit page

Write logic first, then details.

def edit_stu(request): """ Editorial Logic """ if request.method == "GET": # If it is a get request, the page is accessed on behalf of the user # We should return to the look of this page. return render(request, "edit_stu.html") else: # If it is a post request, it represents the user submitting data. # Modifying data on behalf of users # We should update the database. # After successful modification, page Jump id = request.GET.get("id") sql = "update student set k1=v1, k2=v2, kn=vn where id = ".format(id=id) return redirect("/stu_list/")
Template layout

,
Because you need to fill in the original data on the edit page

Use template language to take the lead

Then the data is passed from the back end to the front end.

Backend data sources


Each Edit button is placed with get parameters in the layout.

Each Edit button corresponds to the id value of the current user

Considerable

In the back end

Using id to search a set of data of users from database

Then the whole set of data is passed to the front end.

The front end fills in content as needed

View functions take user data


The get request must have parameters with it

The back end is in the get branch. Take it directly.

Take a check.

Check back

Template Language for Gender Judgment

If the student's gender is the same as the value of option

Just add a selected attribute to the tag

{% if condition%} Code {% elif condition 2%} Code 2 {% else %} Code {% endif %}

Front-end code

<!DOCTYPE html> <!--Authors: pyhui--> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {{ stu_dict }} <form action="" method="post"> <p>Full name:<input type="text" name="name" value="{{ stu_dict.name }}"></p> <p>Gender: <select name="gender" id=""> <option value="male" {% if stu_dict.gender == 'male' %} selected {% endif %}>male</option> <option value="female" {% if stu_dict.gender == 'female' %} selected {% endif %}>female</option> <option value="neutral" {% if stu_dict.gender == 'neutral' %} selected {% endif %}>neutral</option> <option value="secrecy" {% if stu_dict.gender == 'secrecy' %} selected {% endif %}>secrecy</option> </select> </p> <input type="submit" value="Confirmation of modifications"> </form> </body> </html>

Complete code

back-end

def edit_stu(request): """ Editorial Logic """ if request.method == "GET": # If it is a get request, the page is accessed on behalf of the user # We should return to the look of this page. # Get the user's id number id = request.GET.get("id") # Query User Information sql = "select * from student where id = %s" % id print(sql) stu_dict = db.select_one(sql) print(stu_dict) send_dict = { "stu_dict": stu_dict } return render(request, "edit_stu.html", send_dict) else: # If it is a post request, it represents the user submitting data. # Modifying data on behalf of users # We should update the database. # After successful modification, page Jump get_data = request.GET post_data = request.POST print(get_data, post_data) # Get the user's POST data # Get the parameters from get, and take id id = request.GET.get("id") # Get the parameters from post, name and gender name = request.POST.get("name") gender = request.POST.get("gender") # Operation of database sql = "update student set name='', gender='' where id = ".format(id=id, gender=gender, name=name) print("Executing sql Sentence\n",sql) db.commit_data(sql) return redirect("/stu_list/")


Edit pages

9 August 2019, 04:59 | Views: 6349

Add new comment

For adding a comment, please log in
or create account

0 comments