django学习手记(四)---基本步骤三(视图,模板基本操作)

原创
小哥 3年前 (2022-11-16) 阅读数 10 #大杂烩

12、视图,13,模板基本使用。

在firstapp/models.py向文件中添加表(class)

1、在settings.py在内部,修改数据库名称和数据库密码
2,删除迁移文件0001_initial.py
3,在数据库中创建与第一步相对应的数据库。
4,执行生成迁移文件。python manage.py makemigrations
5,执行迁移python manage.py migrate
6、进入sunck数据库(此自定义),show tables;您可以看到数据库中还有一个表。
7、如果firstapp/models.py如果要重新创建或修改字段或将字段添加到表中,请尝试将其全部删除、完全删除并删除迁移文件。0001_initial.py,删除数据库,再试一次
6,启动服务python manage.py runserver
7,浏览器测试
8,创建管理员python manage.py createsuperuser

12,视图的基本用法:

1、概述

在django在中,视图对。web请求响应
视图就是python的功能,在views.py在文件中定义

2,定义视图

from django.http import HttpRespones
def index(request):
    return HttpResponse("Hello, world. Youre at the polls index.")

3、配置url

(1)修改project目录下的urls.py文件

from django.contrib import admin
from django.urls import include, path
urlpatterns = [
    path(firstapp/, include(firstapp.urls)),
    path(admin/, admin.site.urls),
]

(2)在firstapp在应用程序在目录下创建urls.py文件

from django.urls import path
from . import views
urlpatterns = [
    path(, views.index, name=index),
]

现在您已经连接了索引视图。URLconf在里面验证它是否使用以下命令:中。project目录下的输入:python manage.py runserver
在浏览器中转到。http://localhost:8000/firstapp/,您将看到文本“Hello, world. You’re at the polls index.“。这是您在“索引”视图中定义的内容。

想要在http://localhost:8000/firstapp/输入数字,例如2

http://localhost:8000/firstapp/2/
1,定义视图

from django.shortcuts import render

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. Youre at the polls index.")

#这是新添加的代码    
def detail(request,num):
    return HttpResponse("detail-%s"%num)

2、 (1)project目录下的urls.py不需要重新配置文件。
(2)在firstapp在应用程序目录下编辑urls.py文件

from django.urls import path
from . import views
urlpatterns = [
    path(, views.index, name=index),
    path(/, views.detail, name=detail),#这是新添加的代码
]

在网址上http://localhost:8000/firstapp/2 得到下图

想要在http://localhost:8000/firstapp/输入数字/重新输入号码
例如http://localhost:8000/firstapp/100/2
1,定义视图

from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. Youre at the polls index.")
#以下函数需要在上面的数字上添加一个数字num2
def detail(request,num,num2):
    return HttpResponse("detail-%s-%s"%(num,num2))

2、 (1)project目录下的urls.py不需要重新配置文件。
(2)在firstapp在应用程序目录下编辑urls.py文件

from django.urls import path
from . import views
urlpatterns = [
    path(, views.index, name=index),
    path(/, views.detail, name=detail),#将另一个加到数字上,并用斜线分隔。请注意,第一对单引号是路径。
]

13,模板的基本使用

1、概述

模板是HTML可以根据视图中传递的数据填充页面。

2,创建模板目录

创建templates目录,为目录下的相应项目创建模板目录(project/templates/firstapp)

【在project在目录下创建templates文件(模板),在templates目录,创建firstapp文件,对应firstapp应用程序视图,可以有多个应用程序视图],]

D:\program\python3\Django_learning\project> tree . /F

3,配置模板路径

修改settings.py文件TEMPLATES
BASE_DIR就是project目录,D:\program\python3\Django_learning\project

import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#project目录#D:programpython3Django_learningproject

settings.py文件下TEMPLATES中

DIRS: [os.path.join(BASE_DIR,templates)],#模板的目录可用。

4、定义grades.html和students.html模板(在templates/firstapp文件下)

模板语法 ------
1、{ {它可以是输出值、变量或 对象.属性}}-如果要在页面上显示值,请使用python代码
2、{%执行代码段%}

第一部分

http://localhost:8000/firstapp/
(1)写index.html模板
project/templates/firstapp/index.html文件




    
    首页
    
    
    
          
	      
	


    

sunck is good man

#这是网页上显示的内容。

(2)定义视图

from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
    #return HttpResponse("Hello, world. Youre at the polls index.")
    return render(request,firstapp/index.html)

(3)配置urls.py(firstapp文件下)

from django.urls import path
from . import views
urlpatterns = [
    path(, views.index, name=index),

]

(4)显示如下

一般来说URL,我们写道:

urlpatterns = [
path(正则表达式, views查看函数、参数、别名),
]
参数说明:
1,正则表达式字符串。
2,一个可调用的对象,通常是视图函数或指定视图函数路径的字符串。
3,传递给视图函数的可选默认参数(字典形式)
4,可选name参数(别名)

第二部分

1,在(模板)中templates/firstapp添加并保存在文件下grades.html和students.html

  1. 写grades.html和students.html模板
  2. 定义视图
  3. 配置url

grades.html




    
    学生信息
    
    
    
          
	      
	


    

学生信息列表

    {%for student in students%}
  • {{student.sname}}--{{student.scontend}}--{{student.sgrade}}
  • {%endfor%}

students.html




    
    班级信息
    
    
    
          
	      
	


    

班级信息列表

2、在project/urls.py在文件下添加以下代码。

from django.contrib import admin
from django.urls import include, path,re_path
urlpatterns = [
    path(firstapp/, include(firstapp.urls)),
    path(admin/, admin.site.urls),  
]

3、在firstapp/views.py在文件下添加以下代码。

from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from . models import Grades
def grades(request):
    #转到模板以获取数据
    gradesList =Grades.objects.all()
    #将数据传递到模板,模板将再次渲染页面,并将渲染的页面返回给浏览器。
    return render(request,firstapp/grades.html,{"grades":gradesList})
from .models import Students
def students(request):
    studentsList=Students.stuObj2.all()
     return render(request,firstapp/students.html,{"students":studentsList})

4、在firstapp/urls.py在文件下添加以下代码。

from django.urls import path
from . import views
urlpatterns = [
    path(, views.index, name=index),
    path(grades/,views.grades),
    path(students/,views.students),
]

5,输入URL http://localhost:8000/firstapp/students/


6,输入URL http://localhost:8000/firstapp/grades/

第三部分

想知道班上有哪些学生
1,定义视图

from .models import Students,Grades
def gradesStudents(request,num):
    #获取相应的类对象。
    grade=Grades.objects.get(pk=num)#取出班级序列号num的班级
    #列出全班学生的名单。
    studentsList=grade.students_set.all()
    return render(request,firstapp/students.html,{"students":studentsList})

2、配置firstapp/urls.py文件

path(grades/,views.gradesStudents),#注意最后一个逗号


3,输入URL http://localhost:8000/firstapp/grades/1

4,输入URL http://localhost:8000/firstapp/grades/2

5,输入URL http://localhost:8000/firstapp/grades/3

6,输入URL http://localhost:8000/firstapp/grades/4

7,或输入URL http://localhost:8000/firstapp/grades/ 然后直接单击要查询的班级学生,结果将与上面的相同。3、4、5、6结果一样

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除