版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gang950502/article/details/90141235
django - (四) 创建第一个网页
- 创建第一个视图函数(student_sys/student/view.py)
from .models import Student def index(request): # 获取student内存的所有对象 students = Student.objects.all() # 使用render渲染index.html return render(request,'student/index.html',context={'students':students})
- 创建一个网页名称是index.html(student_sys/templates/student/index.html)
<!DOCTYPE html> <html> <head> <title>学员管理系统</title> </head> <body> <ul> {% for student in students %} <li>{{ student.name}} - {{ student.get_status_display }}</li> {% endfor %} </ul> </body> </html>
- 增加这个view的路由(student_sys/student_sys/urls.py)
from django.contrib import admin from django.urls import path from django.conf.urls import url from student.views import index urlpatterns = [ url(r'^$',index,name='index'), path('admin/', admin.site.urls), ]
- 增加html的搜索路径(修改student_sys/student_sys/setting.py中TEMPLATES:DIRS属性)
'DIRS': ['templates',],
- 启动工程,打开浏览器查看127.0.0.1:8000