1.文件设置
1)djg1->settings->TEMPLATES->DIRS:
指定templates文件的路径,用于保存html文件
'DIRS': [os.path.join(BASE_DIR,'templates')]
2)djg1->settings->末尾添加:
添加static文件的路径,用于存放js,css,img等文件
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
2.创建路由分支
djg1->urls->urlpatterns:
url(r'app/',include('app.urls',namespace='app')),
app->urls-urlpatterns:
例:
urlpatterns=[
url(r'^stu/',views.index,name='index'),
]
3.app->urls.py
from django.conf.urls import url
from app import views
urlpatterns=[
url(r'^stu/',views.index,name='index'),
# url(r'^del_stu/(\d+)/',views.del_stu,name='del_stu'),
url(r'^del_stu/(?P\d+)/', views.del_stu, name='del_stu'),
url(r'^look_stu/(?P\d+)/',views.look_stu,name='look_stu'),
url(r'^re_stu/',views.re_stu,name='re_stu'),
]
4.app->views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from app.models import StudentInfo, Student, Grade, Course
#导出数据库数据
def index(request):
if request.method=='GET':
# 返回字符
# return HttpResponse('hello')
# 返回html页面
stus=Student.objects.all()
# return render(request,'index.html',{'stus':stus})
return render(request, 'stus.html', {'students': stus})
#删除数据
def del_stu(request,s_id):
if request.method=='GET':
# 删除方法
# 1.获取url中的id值
# id=request.GET.get('id')
# 2.获取id对应的学生对象
stu=Student.objects.get(pk=s_id)
# 3.对象.delete()
stu.delete()
# return HttpResponseRedirect('/app/stu/')
return HttpResponseRedirect(reverse('app:index'))
# return HttpResponseRedirect(index())
#查询数据
def look_stu(request,s_id):
if request.method=='GET':
stu=Student.objects.get(pk=s_id)
stu_info=stu.stu_info
g=stu.g
c=stu.c.all()
# return HttpResponse(s_id)
return render(request,'sel.html',{'stu':stu,'stu_info':stu_info,'g':g,'c':c})
#返回操作
def re_stu(request):
if request.method=='GET':
return HttpResponseRedirect(reverse('app:index'))
5.templates
1)base.html创建父类模板
{% block title %}
{% endblock %}
{% block extCss %}
{% endblock %}
{% block extJs %}
{% endblock %}
{% block content %}
{% endblock %}
2)base_main.html添加js样式
{% extends 'base.html' %}
{% block extJs %}
{% endblock %}
3.index.htm 关于lDjango的用法
Title
{# 方法一: #}
{# 方法二: #}
{% load static %}
你好啊
order
id
name
age
{# sex #}
{% comment %}多行注解 软件不会解析{% endcomment %}
{# 单行注解 软件不会解析 #}
{% for stu in stus %}
{{forloop.revcounter0}}
{{stu.id}}
{{stu.s_name}}
{{stu.s_age}}
{% comment %}
{{stu.s_sex}}
{% endcomment %}
{% endfor %}
3)stu.html显示数据
{% extends 'base_main.html' %}
{% block title %}
学生列表页面
{% endblock %}
{% block extJs %}
{{ block.super }}
{% endblock %}
{% block content %}
order
id
name
age
operate
{% for stu in students %}
{{forloop.revcounter0}}
{{stu.id}}
{{stu.s_name}}
{{stu.s_age}}
删除
|
查看
{% endfor %}
{% endblock %}
4)sel.html 显示查询数据库
{% extends 'base_main.html' %}
{% block title %}
查询学生信息
{% endblock %}
{% block content %}
{{stu.id}}
{{stu.s_name}}
{{stu.s_age}}
{{stu.s_sex}}
{{stu.creat_tim}}
{{stu.operate.time}}
{{stu.chinese}}
{{stu.math}}
{{stu_info.phone}}
{{stu_info.address}}
{{g.g_name}}
{% for data in c %}
{{data.c_name}}
{% endfor %}
返回
{% endblock %}