1.通过pycharm创建django项目
2.url中有 all和add分别为展示所有学生信息和添加学生信息
3.学生信息需包含name、age、classe
4.在浏览器中输入 127.0.0.1:8001/all 显示所有学生信息
5.默认已在视图中添加一些学生信息
6.在浏览器中输入 127.0.0.1:8001/add?name=meimei&age=23&classe=1309a 添加学生信息
7.添加完学生信息需自动跳转到 all链接显示所有学生信息,包括新添加的
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")