Python学习之路—2018/6/19

Posted exburner

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习之路—2018/6/19相关的知识,希望对你有一定的参考价值。

Python学习之路—2018/6/19

1.注册自定义转化器

converts.py

class Birthday:
    regex = ‘[0-9]{8}  # 匹配规则

    def to_python(self, value):  # 匹配的字符串返回具体的变量值,传递到对应的视图函数中
        return int(value)

    def to_url(self,value):  # 反向解析
        return "%04d" % value

urls.py

from django.urls import path, register_converter
from app01 import views, urls, converts


register_converter(converts.Birthday, "birth")  
urlpatterns = [
    path(‘gyq/<birth:year>‘, views.birthday)
]

views.py

def birthday(request, date):
    return HttpResponse(date)

2.HttpRequest对象

url组成:协议/IP/端口/path/请求数据

request常用属性

HttpRequest.GET 包含了HTTP GET的所有参数

HttpRequest.POST 如果请求中包含表单数据,将这些数据包装成QueryList对象

HttpRequest.method Http请求的方法(get/post)

HttpRequest.encoding 提交的数据编码

request常用方法

HttpRequest.get_full_path() 返回path ,包括查询字符串。

HttpRequest.get_full_path() 判断请求是否为XMLHttpRequest发起的

视图响应对象

响应对象主要有三种形式:

  • HttpResponse()
  • render()
  • redirect()

HttpResponse()括号内直接跟一个具体的字符串作为响应体

render()

render(request, template_name[, context])

request: 用于生成响应的请求对象。

template_name:要使用的模板的完整名称,例如index.html

context:添加到模板上下文的一个字典。默认是一个空字典。如果字典中的某个值是可调用的,视图将在渲染模板之前调用它。

以上是关于Python学习之路—2018/6/19的主要内容,如果未能解决你的问题,请参考以下文章

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

机器学习之路: python 实践 word2vec 词向量技术

python学习之路01

python之路之前没搞明白4面向对象(封装)

python 机器学习有用的代码片段

Python学习之路——模块