1. Create a Django project from Pycharm
All and add are displayed for all student information and student information in 2.url
3. Student information must include name, age, Classe
4. Enter 127.0.0.1:8001/all in the browser to display all student information
5. By default, some student information has been added to the view
6. Enter 127.0.0.1:8001/ADD?NAME=MEIMEI&AGE=23&CLASSE=1309A in the browser to add student information
7. After adding student information, you should automatically jump to the all link to display all student information , including the newly added
urls.py
Urlpatterns = [
URL (r ' ^admin/', admin.site.urls),
URL (r ' ^all/$ ', views.all_stu,name= ' all_student '),
URL (r ' ^add/$ ', views.add_stu,name= ' add_student '),
]
All.html
{% for Stu in allstr%} {% ENDFOR%}
name |
| Age
class |
{{stu.0}} |
{{stu.1}} |
{{stu.2}} |
views.py
From django.shortcuts import Render,httpresponse,httpresponseredirect
#? name= ' Xixi ' &age=11&class=1411
# Create your views here.
Allstr_list = [
[' Zhangsan ', 23, ' 1401 '],
[' Lisi ', 23, ' 1401 '],
[' Wangwu ', 23, ' 1401 '],
]
def all_stu (Request):
Allstr=allstr_list
return render (Request, ' all.html ', locals ())
def add_stu (Request):
If not request. Get.get (' name '):
Allstr=allstr_list
return render (Request, ' all.html ', locals ())
name = Request. get[' name ']
Age = Request. Get[' age ']
Classes = Request. Get[' class ']
New_stu = [Name,age,classes]
Global Allstr_list
Allstr_list.append (New_stu)
Print "----------------------"
Print Allstr_list
Print "----------------------"
Return Httpresponseredirect ("/all")
Manage Small projects