1.文件設(shè)置
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.創(chuàng)建路由分支
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<s_id>\d+)/', views.del_stu, name='del_stu'),
url(r'^look_stu/(?P<s_id>\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
#導(dǎo)出數(shù)據(jù)庫(kù)數(shù)據(jù)
def index(request):
if request.method=='GET':
# 返回字符
# return HttpResponse('hello')
# 返回html頁(yè)面
stus=Student.objects.all()
# return render(request,'index.html',{'stus':stus})
return render(request, 'stus.html', {'students': stus})
#刪除數(shù)據(jù)
def del_stu(request,s_id):
if request.method=='GET':
# 刪除方法
# 1.獲取url中的id值
# id=request.GET.get('id')
# 2.獲取id對(duì)應(yīng)的學(xué)生對(duì)象
stu=Student.objects.get(pk=s_id)
# 3.對(duì)象.delete()
stu.delete()
# return HttpResponseRedirect('/app/stu/')
return HttpResponseRedirect(reverse('app:index'))
# return HttpResponseRedirect(index())
#查詢數(shù)據(jù)
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創(chuàng)建父類模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
{% block title %}
{% endblock %}
</title>
{% block extCss %}
{% endblock %}
{% block extJs %}
{% endblock %}
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
2)base_main.html添加js樣式
{% extends 'base.html' %}
{% block extJs %}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"type="text/javascript"></script>
{% endblock %}
3.index.htm 關(guān)于lDjango的用法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{# 方法一: #}
<!--<script src="/static/js/index.js"></script>-->
<!--<link rel="stylesheet" href="/static/css/index.css">-->
{# 方法二: #}
{% load static %}
<script src="{% static 'js/index.js' %}"></script>
<link rel="stylesheet" href="{% static 'css/index.css' %}">
</head>
<body>
<h2>你好啊</h2>
<table>
<thead>
<th>order</th>
<th>id</th>
<th>name</th>
<th>age</th>
{# <th>sex</th> #}
</thead>
<tbody>
{% comment %}多行注解 軟件不會(huì)解析{% endcomment %}
{# 單行注解 軟件不會(huì)解析 #}
<!--軟件可以解析-->
{% for stu in stus %}
<tr {% ifequal stu.id 4 %} style="color:red;"{% endifequal %}>
<td>{{forloop.revcounter0}}</td>
<td {% if forloop.first %} style="color:blue"{% endif %}>{{stu.id}}</td>
<td {% if stu.id == 3 %} style="color:green;"{% endif %}>{{stu.s_name}}</td>
<td {% if forloop.last %} style="color:red"{% endif %}>{{stu.s_age}}</td>
{% comment %}
<td>{{stu.s_sex}}</td>
{% endcomment %}
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
3)stu.html顯示數(shù)據(jù)
{% extends 'base_main.html' %}
{% block title %}
學(xué)生列表頁(yè)面
{% endblock %}
{% block extJs %}
{{ block.super }}
<script src="/static/js/index.js"></script>
{% endblock %}
{% block content %}
<table>
<thead>
<th>order</th>
<th>id</th>
<th>name</th>
<th>age</th>
<th>operate</th>
</thead>
<tbody>
{% for stu in students %}
<tr>
<td>{{forloop.revcounter0}}</td>
<td >{{stu.id}}</td>
<td >{{stu.s_name}}</td>
<td >{{stu.s_age}}</td>
<td>
<!--<a href="/app/del_stu/?id={{stu.id}}">刪除</a>-->
<!--<a href="{% url 'app:del_stu' stu.id %}">刪除</a>-->
<a href="{% url 'app:del_stu' stu.id %}">刪除</a>
|
<a href="{% url 'app:look_stu' stu.id %}">查看</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
4)sel.html 顯示查詢數(shù)據(jù)庫(kù)
{% extends 'base_main.html' %}
{% block title %}
查詢學(xué)生信息
{% endblock %}
{% block content %}
<p>{{stu.id}}</p>
<p>{{stu.s_name}}</p>
<p>{{stu.s_age}}</p>
<p>{{stu.s_sex}}</p>
<p>{{stu.creat_tim}}</p>
<p>{{stu.operate.time}}</p>
<p>{{stu.chinese}}</p>
<p>{{stu.math}}</p>
<p>{{stu_info.phone}}</p>
<p>{{stu_info.address}}</p>
<p>{{g.g_name}}</p>
{% for data in c %}
<p>{{data.c_name}}</p>
{% endfor %}
<a href="{% url 'app:re_stu'%}">返回</a>
{% endblock %}