美文网首页
Day4-课堂笔记-Django4

Day4-课堂笔记-Django4

作者: 晓晓的忍儿 | 来源:发表于2018-09-15 09:12 被阅读0次

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<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

#导出数据库数据
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创建父类模板

<!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 关于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 %}多行注解 软件不会解析{% endcomment %}
        {#  单行注解 软件不会解析 #}
        <!--软件可以解析-->
        {% 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显示数据

{% extends 'base_main.html' %}
{% block title %}
学生列表页面
{% 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 显示查询数据库

{% extends 'base_main.html'  %}

{% block title %}
查询学生信息
{% 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 %}

相关文章

网友评论

      本文标题:Day4-课堂笔记-Django4

      本文链接:https://www.haomeiwen.com/subject/wdjzgftx.html