1.配置urls的方式
1.正向
python3中的配置:
1.位置传参:参数依次取值 re_path(‘query/(\d{2})’,views.index1)
2.关键字传参:?P<关键字> re_path(‘query1/(?P[0-9]{4})/(?P[0-9]{2})’,views.index2)
例子:
方式2:
方式3:
方式4:
2.逆向解析
1.修改url中的name属性,在超链接中首先匹配urlspattens中有没有这个url,有的话返回,对于的函数。
urls 修改
re_path('query3/(\d{2})',views.index4,name = 'q'),
html
<a href={% url 'q' n %}>超链接</a>
index中返回超链接
def index(request):
return render(request,'query.html',{'n':16})
2.通过
重定向的方式(重新访问)
urls中
path('query4/',views.index5)
views中
def index5(request):
return HttpResponseRedirect(reverse('q',args=(66,)))
#'q'为链接名字,args是元组参数
3.修改跟路由
根urls:
path(‘student/’,include(‘stu.urls’,namespace=‘stus’)),
子路由:
添加 app_name= ‘stu’
设置访问名字
path(‘query5/’,views.index6,name = ‘p’)
html中加载
<a href={% url ‘stus:p’ %}>超链接2
2.HttpRequest和HttpResponse
1.request.scheme 协议类型:http
2.request.boby 请求的实体内容
3.request.path 请求地址
4.request.method 请求方式
5.request.GET.get 返回的是queirDict
6.request.COOKIES
7.request.FILES 页面中有上传文件
8.request.META 请求报文
9.request.get_host() 主机名和端口号
10.request.get_full_path() 全路径
1.HttpResponse(‘内容’,conten_type = ‘text/plain’) 文本类型
重新改变报文信息:报文中Server改为wisp
response.setitem(‘Server’,‘wisp’)
3 上传文件
1.原始上传
上传文件必须是post方法
必须写 method=“post” enctype=“multipart/form-data” (不对字符编码)
1.html 中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/student/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>
<label for="ua">姓名:</label><input type="text" name="uname" id="ua"/>
</p>
<p>
<label for="ph">头像:</label><input type="file" name="photo" id="ph"/>
</p>
<p>
    <input type="submit" value="注册"/>
</p>
</form>
</body>
</html>
2.views中加入
(1)一次读取 (2)分块读取
#原始上传
def index_views(request):
if request.method=='GET':
return render(request,'index.html')
elif request.method=='POST':
uname = request.POST.get('uname','')
photo = request.FILES.get('photo','')
import os
if not os.path.exists('media'):
os.makedirs('media')
with open(os.path.join(os.getcwd(),'media',photo.name),'wb') as fw:
# #一次读取
# fw.write(photo.read())
#分块读取
for ck in photo.chunks():
fw.write(ck)
return HttpResponse('上传成功')
else:
return HttpResponse('当前访问量较大')
2.用django 框架上传
1.修改setting配置
#(读取文件)指定s上传文件的存储相对路径
MEDIA_URL = '/media/'
#(上传文件)指定s上传文件的存储,决定路径
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
2.写model类
class Student(models.Model):
sno = models.AutoField(primary_key=True)
sname = models.CharField(max_length=30)
photo = models.ImageField(upload_to='imgs')
def __str__(self):
return 'Student:%s'%self.sname
3.修改html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/student/upload/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>
<label for="ua">姓名:</label><input type="text" name="uname" id="ua"/>
</p>
<p>
<label for="ph">头像:</label><input type="file" name="photo" id="ph"/>
</p>
<p>
    <input type="submit" value="注册"/>
</p>
</form>
</body>
</html>
4.修改路由
path(‘upload/’,views.upload_views),
3.显示列表
1.改路由
path(‘showall/’,views.showall)
2.读取学生信息
#显示
def showall(request):
stus = Student.objects.all()
return render(request,'showall.html',{'stus':stus})
3.页面显示
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table width="500px" border="1" cellspacing="0">
<tr>
<th>编号</th>
<th>姓名</th>
<th>头像</th>
<th>操作</th>
</tr>
{% for stu in stus %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ stu.sname }}</td>
<td><img style="width: 200px;" src="{{ MEDIA_URL }}{{ stu.photo }}"/></td>
<td><a href="/student/download/?photo={{ stu.photo }}">下载</a></td>
</tr>
{% endfor %}
</table>
</body>
</html>
4.根路由中加入
from django.views.static import serve
from .settings import DEBUG,MEDIA_ROOT
if DEBUG:
urlpatterns+=path('media/(?P<path>.*)/$', serve, {"document_root": MEDIA_ROOT}),
5.setting中修改TEMPLATES
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media'
],
},
运行结果
实验做了两次,第二次实验室没清理第一次实验是数据内容。