程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django learning based on pychar -- user management DEMO

編輯:Python

Since I learned Django after , I'm really pink Silver horn King Wu Peiqi , How to put it? , Quite emotional , Just study hard .

Put this to use , Put this to use , To learn the Django Medium ORM, How can we not write a small case ?

be based on Pycharm Of Django Study

    • User management needs analysis
    • User management
      • Show user list
      • Add users
      • Delete user

So let's write a small user management demo Well !

User management needs analysis

demand :

  1. Show user list
  2. Add users
  3. Delete user

User management

Then write it step by step !

In fact, writing code is like this , Step by step analysis , First write a basic function , Then go to a little bit of improvement and optimization , So don't be afraid of trouble !

urls.py —— views.py —— html

Show user list

def info_list(request):
# Get all the information in the database
data_list = models.UserInfo.objects.all()
# Test the data that needs to be passed in the back-end function Make sure the data passed is correct Then migrate to the front end And change the corresponding syntax rules
print(data_list)
# Rendering Back to the front end
return render(request, "info_list.html", {"data_list": data_list})

Show the process :

  1. Get database data
  2. Front end test data format and content
  3. Render and return to the page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> User list </h1>
<!-- Add a button to the list page You can jump to the add page at one click -->
<!--http://127.0.0.1:8000/info/add/ Because it's your own website You don't need to write a domain name -->
<div >
<a href="/info/add/"> Add users </a>
</div>
<table border="1" >
<thead>
<tr>
<th>ID</th>
<th> full name </th>
<th> password </th>
<th> Age </th>
<th> operation </th>
</tr>
</thead>
<!-- First test with static data -->
<!--<tbody>
<tr>
<td>1</td>
<td> Wang Xiaoman </td>
<td>123</td>
<td>20</td>
</tr>
</tbody>-->
<tbody>
{% for data in data_list %}
<tr>
<td>{
{data.id}}</td>
<td>{
{data.name}}</td>
<td>{
{data.password}}</td>
<td>{
{data.age}}</td>
<td>
<a href="/info/delete/?nid={
{data.id}}"> Delete </a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

When writing front-end pages , Also need attention , First, use static data to demonstrate , Then replace it with the data from the back end , It's also easy to test , Because writing code is not done overnight .

Add users

def info_add(request):
if request.method == "GET":
return render(request, "info_add.html")
# If it is POST Get user data
name = request.POST.get("user")
password = request.POST.get("password")
age = request.POST.get("age")
# Add to database
models.UserInfo.objects.create(name=name, password=password, age=age)
return redirect("/info/list")

When testing page Jump , It must be a response , See the three responses above .

If you are still testing the function , Can respond to text , Input again url Go to the specified page to view .

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> Add users </h1>
<!--action If you don't write Then send to the current address post request Don't write nonsense in this place Or here url It doesn't match in the function That's a mistake -->
<form method="post">
{% csrf_token %}
<label> user name :<input type="text" name="user" placeholder=" user name "> </label>
<label> password :<input type="password" name="password" placeholder=" password "> </label>
<label> Age :<input type="text" name="age" placeholder=" Age "> </label>
<input type="submit" value=" Submit ">
</form>
</body>
</html>



Delete user


We are assuming such a function : When user input http://127.0.0.1:8000/info/delete/?nid= In this way url when , Delete the corresponding user entered by the user in the database id Information about , And jump to the user list display page .

def info_delete(request):
nid = request.GET.get("nid")
models.UserInfo.objects.filter(id=nid).delete()
return redirect("/info/list")

Because if the user keeps typing url, It may be a little cumbersome , So we are in the front list , An additional column is added , In this case , You can splice through links url And jump .



Of course , The above is just to demonstrate the case , Did not beautify page , Later, we will write a comprehensive big project .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved