Django_day04

2018-09-13  本文已影响0人  HavenYoung
Django
基本结构
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 %}
    学生列表界面
{% 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>序号</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对应的学生对象
        stu = Student2.objects.get(pk=s_id)
        # 3.对象.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})

知识小结:
1.forloop
{{ forloop. counter }}表示当前是第几次循环,从1开始
{{ forloop. countere }}表示当前从第几次循环,从0开始
{{forloop. revcounter}}表示当前是第几次循环,倒着数数,到1停
{{forloop. reJcounter0}}表示当前是第几次循环,倒着数数,到0停
{{forloop. first}}是否是第一个
布尔值
{{forloop. last}}是否是最后一个
布尔值

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中设置
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>

上一篇 下一篇

猜你喜欢

热点阅读