6-视图-1
view.py
from django.shortcuts import render from django.http import HttpResponse # Create your views here. def first_view(request): html='<p>Hello World!</p>' return HttpResponse(html)
urls.py
from django.contrib import admin from django.urls import path from firstApp import views urlpatterns = [ path('admin/', admin.site.urls), path('view/hello/',views.first_view) ]

为每一个应用单独定制一个urls.py,并集中通过include汇总到项目中的urls.py中
为应用新建一个urls.py
from django.urls import path from firstApp import views urlpatterns = [ path('hello/',views.first_view), ]
项目总urls.py
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('firstapp/',include('firstApp.urls')) ]
这样,以firstapp开头的请求都会转移给firstapp应用的urls.py去处理
HttpRequest
1、method
from django.http import HttpResponse # Create your views here. def first_view(request): html='<p>Hello World!请求方式为:'+str(request.method)+'</p>' return HttpResponse(html)

2、@require_http_methods('POST')装饰器规定该视图使用什么方式去访问,当种访问方式可用List(列表[ 'GET','POST',]);也可以使用@require_POST或者@require_GET规定
@csrf_exempt#装饰器 @require_http_methods('POST') def first_view(request): html='<p>Hello World!请求方式为:'+str(request.method)+'</p>' return HttpResponse(html)

用其他方式去访问时,会出现405错误

3、request.GET,request.POST获取参数值
@csrf_exempt#装饰器 @require_http_methods(['POST','GET']) def first_view(request): parameter=request.GET.get('name','无参数')#地址中无name时,则会返回第二个字段的信息;如果不想让别人获取到信息,可以使用request.GET['name'],此方法当地址中无name时,会返回一个错误 html='<p>Hello World!请求方式为:'+str(request.method)+'</p><span>获取到了:'+parameter+'</sapn>' return HttpResponse(html)


4、FILES(上传文件)
5、META(包含HTTP请求的头部所有信息)
| 属性 | 描述 |
| CONIENT_LENGTH | 标识请求消息正文的长度,对于POST,这个是必须的 |
| CONIENT_TYPE | 请求头的MIME类型 |
| HTTP_HOST | 客户端发送的HTTP主机头 |
| HTTP_USER_AGENT | 用来标识浏览器的类型,如果要做兼容的话,这个还是要的 |
| REMOTE_ADDR | 客户端的IP地址 |
| REMOTE_GOST | 客户端的主机名 |
| REQUEST_METHOD | 标识HTTP的请求方法 |
| SERVER_NAME | 服务器的主机名 |
| SERVER_PORT | 服务器的端口号 |
HTTPResponse
1、status_code
| 200 | 请求成功 |
| 400 | 错误请求 |
| 403 | 禁止访问 |
| 404 | 请求的资源不存在 |
| 500 | 服务器内部错误 |
2、content(响应内容的二进制文件);write方法
@csrf_exempt#装饰器 @require_http_methods(['POST','GET']) def first_view(request): if HttpResponse.status_code ==200: parameter=request.GET.get('name','无参数')#地址中无name时,则会返回第二个字段的信息 html='<p>Hello World!请求方式为:'+str(request.method)+'</p><span>获取到了:'+parameter+'</sapn>' response=HttpResponse(html) response.write(HttpResponse.content) response.write(HttpResponse.status_code) else: html = '<p>请求状态为:'+HttpResponse.status_code+'</p>' return HttpResponse(html)

基于类的视图
重新定义views.py中的内容
class firstView(View): html='欢迎%s来到第一个类视图' def get(self,request): return HttpResponse(self.html%('诺言GET')) def post(self,request): return HttpResponse(self.html%('诺言POST')) @method_decorator(csrf_exempt)#对于def的装饰器不能直接用在class上,需用method_decorator转换为类装饰器 def dispatch(self, request, *args, **kwargs):#重写父类的方法dispatch,会根据当前的请求方式分发请求,如果没有,则会返回HttpResponseNotAllowed return super(firstView,self).dispatch(request, *args, **kwargs)
urls.py
from firstApp.views import firstView urlpatterns = [ path('httpclass/',firstView.as_view()),#因为会将请求发送给一个单独的函数而不是类,所以需要用到views的as_view方法 ]

动态路由(除了HttpRequest之外,还可以有其他的参数,URL就不是固定的)
def time_time(request,name,year,month,day): html = '%s到来的时间为:%s-%s-%s' return HttpResponse(html%(name,year,month,day))
urls.py
path('msg/<name>/<int:year>/<int:month>/<int:day>/',views.time_time)#利用转换器指定该位置需要什么字符

如果地址中参数的位置输入的格式不对或者是少了参数的话,就会

| 转换器 | 含义 |
| str | 匹配除了“/”的任意非空字符,默认转换器,如果没有指定转换器,如<name>等价于<str:name> |
| int | 数字型的值 |
| slug | 匹配字母、数字、连字符和下划线组成的字符串 |
| uuid | 匹配格式化的UUID |
| path | 匹配任意的非空字符串,包含“/” |
自定义校验规则
from django.urls.converters import IntConverter #自定义转换器 class Month_Check(IntConverter):#对月份字符形成校验 regex = '0?[1-9]|1[0-2]' class Year_Check(IntConverter):#对年份字符形成校验 regex = '0?0?0?[1-9]|0?0?[1-9][0-9]|0?[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]' class Day_Check(IntConverter): regex = '0[1-9]|1[0-9]|2[0-9]|30'
urs.py中注册定义的校验规则
from django.urls import path, register_converter from firstApp.views import firstView, Year_Check, Day_Check,Month_Check #注册转换器 register_converter(Year_Check,'year') register_converter(Month_Check,'month') register_converter(Day_Check,'day')

当然,视图函数中定义的参数也是可以有默认值的,定义玩之后要在urls.py中可以从新定义一个path,这样就会有两条访问路径了,一条是有默认值,一条没有默认值
正则表达式(python的正则表达式语法为:(?P<name>pattern),name是分组名,pattern是匹配模式)
#分组正则表达式 r=re.compile('(?P<year>[0-9]{4})') s=r.search('2019') print(s.group('year')) print(s.group(1))
当path和转换器都不能满足需求时,可用re_path方法重新定义
urlpatterns = [ path('httpclass/',firstView.as_view()),#因为会将请求发送给一个单独的函数而不是类,所以需要用到views的as_view方法 # path('msg/<name>/<year:year>/<month:month>/<day:day>/',views.time_time), # path('msg/<name>/<year:year>/<month:month>/',views.time_time), re_path('msg/(?P<name>[\u4e00-\u9fa5]{0,4})/(?P<year>[0-9]{4})/(?P<month>0[0-9]|1[0-2])/(?P<day>0[1-9]|1[0-9]|2[0-9]|30)/',views.time_time), ]

给应用firstapp添加视图
首先先创建firstapp/app_service.py
#!/usr/bin/env python # -*- coding:utf-8 -*- def topic_json(topic): ''' 构建model基本信息 :param Topic: :return: ''' return { 'id':topic.id,#id 'title':topic.title, 'user':topic.user.username, 'create_time':topic.create_time.strftime('%Y-%m-%d %H:%M:%S')#对时间处理 }
views.py
from django.http import JsonResponse from firstApp.models import Topic from firstApp.app_service import topic_json def topic_view(request): ''' model :param request: :return: ''' topic_qs=Topic.objects.all()#ORM方法查询所有记录, result={ 'count':topic_qs.count(),#统计数据数量 'info':[topic_json(topic) for topic in topic_qs]#循环topic_qs并将结果赋给topic_json(topic)处理 } return JsonResponse(result)#返回json格式数据
最后配置一下urls.py就可以进行访问了

重新定义app_service.py
def topic_detail_json(topic): ''' 构建topic详细信息 :param topic: :return: ''' comment_qs=Comment.objects.filter(topics=topic)#进行过滤 return { 'id': topic.id, # id 'title': topic.title, 'content':topic.content, 'user': topic.user.username, 'create_time': topic.create_time.strftime('%Y-%m-%d %H:%M:%S'),#对时间处理 'update_time': topic.update_time.strftime('%Y-%m-%d %H:%M:%S'), # 对时间处理 'comments':[comment_detail_json(comment) for comment in comment_qs] } def comment_detail_json(comment): ''' 构建comment基本信息 :param Topic: :return: ''' return { 'id':comment.id,#id 'content':comment.content, 'up':comment.up, 'down':comment.down, 'create_time':comment.create_time.strftime('%Y-%m-%d %H:%M:%S'),#对时间处理 'update_time': comment.update_time.strftime('%Y-%m-%d %H:%M:%S') # 对时间处理 }
views.py
from firstApp.app_service import topic_detail_json def topic_detail(request,topic_id): result=topic_detail_json(Topic.objects.get(pk=topic_id))#根据id进行查询 return JsonResponse(result)
urls.py
path('topic_detail/<int:topic_id>/',views.topic_detail),

(未完待续,,,,,,,)

浙公网安备 33010602011771号