基本結(jié)構(gòu)
day04
|——app
| |——migrations
| | |——__init__.py
| |——__init__.py
| |——admin.py
| |——apps.py
| |——models.py
| |——tests.py
| |——urls.py
| |——views.py
|——day04
| |——__init__.py
| |——settings.py
| |——wsgi.py
|——static
| |——css
| |——images
| |——js
|——templates
| |——html文件
stu.html
{% extends 'base_main.html' %}
{% block title %}
學(xué)生列表界面
{% endblock %}
{% block extJs %}
{# 繼承base_main.html中的jQuery#}
{{ block.super }}
{# 添加新的js鏈接#}
{% load static %}
<script src="{% static 'js/test.js' %}"></script>
{% endblock %}
{% block content %}
<table>
<thead>
<tr>
<td>序號(hào)</td>
<td>id</td>
<td>name</td>
<td>age</td>
</tr>
</thead>
<tbody>
{% for stu in students %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ stu.id }}</td>
<td>{{ stu.s_name }}</td>
<td>{{ stu.s_age }}</td>
<td>
{# <a href="{% url 'app:del_stu' %}?id={{ stu.id }}">刪除</a>#}
<a href="{% url 'app:del_stu' stu.id %}">刪除</a>
<a href="{% url 'app:sel_stu' stu.id %}">查看</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from app.models import Student2
def index(request):
if request.method == 'GET':
stus = Student2.objects.all()
# return render(request, 'index.html', {'students': stus})
# return HttpResponse('hello')
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對(duì)應(yīng)的學(xué)生對(duì)象
stu = Student2.objects.get(pk=s_id)
# 3.對(duì)象.delete()
stu.delete()
# 重定向
return HttpResponseRedirect(reverse('app:index'))
# return HttpResponseRedirect('/app/stu/')
def sel_stu(request, s_id):
if request.method == 'GET':
stu = Student2.objects.get(pk=s_id)
return render(request, 'stu_info.html', {'student': stu})
知識(shí)小結(jié):
1.forloop
{{ forloop. counter }}表示當(dāng)前是第幾次循環(huán)匾二,從1開始
{{ forloop. countere }}表示當(dāng)前從第幾次循環(huán)察藐,從0開始
{{forloop. revcounter}}表示當(dāng)前是第幾次循環(huán)分飞,倒著數(shù)數(shù)浸须,到1停
{{forloop. reJcounter0}}表示當(dāng)前是第幾次循環(huán)删窒,倒著數(shù)數(shù),到0停
{{forloop. first}}是否是第一個(gè)
布爾值
{{forloop. last}}是否是最后一個(gè)
布爾值
2.block
{% block extJs %}
{# 繼承base_main.html中的jQuery#}
{{ block.super }}
{# 添加新的js鏈接#}
{% load static %}
<script src="{% static 'js/test.js' %}"></script>>
{% endblock %}
3.load
在settings.py中設(shè)置
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
在html中配置
{% load static %}
<link rel="stylesheet" href="{% static 'css/index.css' %}">
<script src="{% static 'js/test.js' %}"></script>