网络通讯的本质是socket,从socket封装到MVC模式,参见另外几篇博客。本节笔记整理自Django2.0官方文档。

一、url调度器 - django.urls.path

  django2.0中使用path函数替代url函数。path函数源码如下:

def _path(route, view, kwargs=None, name=None, Pattern=None):
if isinstance(view, (list, tuple)):
# For include(...) processing.
pattern = Pattern(route, is_endpoint=False)
urlconf_module, app_name, namespace = view
return URLResolver(
pattern,
urlconf_module,
kwargs,
app_name=app_name,
namespace=namespace,
)
elif callable(view):
pattern = Pattern(route, name=name, is_endpoint=True)
return URLPattern(pattern, view, kwargs, name)
else:
raise TypeError('view must be a callable or a list/tuple in the case of include().') path = partial(_path, Pattern=RoutePattern) # functools.partial
re_path = partial(_path, Pattern=RegexPattern)

  path函数接收四个参数:route,view,kwargs和name。它用functools.partial装饰了一下,将路由处理类RoutePattern作为参数传递给了Pattern。

  1、path函数的参数[route,view,kwargs,name]

urlpatterns = [
path('homePage', views.homePage),
path('userInfo', views.userInfo, name='userInfo'),
path('blog', views.blog, name='logout', kwargs={'id':10})
]

  route指定url匹配规则并可以从url中获取参数,view返回一个视图函数或者一个url列表(元组),name主要使模板和url解耦,kwargs为视图函数设置参数。

  2、route匹配和获取url参数

  path函数默认使用RoutePattern来匹配url,并从中获取相应参数,该参数需要在视图函数中设置同名形参来接收。

# app01/urls.py
from django.urls import path
from app01 import views
urlpatterns = [
path('items/<name>/<int:id>', views.items_handler),
]
# app01/views.py
from django.shortcuts import HttpResponse def items_handler(request, name, id):
return HttpResponse("{}, {}".format(name, id))

  route可以使用"<val>"获取指定的字符串,甚至可以使用"<type: val>"的方式指定获取的数据类型,参数val需要被接收。

  path函数支持str、int、path、slug、uuid等数据类型。str匹配不包含路径分隔符"/"的非空字符串,path匹配包含路径分隔符"/"的非空字符串,int包含有效的整数。

  也可以自定义数据类型:

from django.urls import path, register_converter
from . import converters, views class FourDigitYearConverter:
regex = '[0-9]{4}'
def to_python(self, value):
return int(value)
def to_url(self, value):
return '%04d' % value
register_converter(converters.FourDigitYearConverter, 'yyyy')
urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<yyyy:year>/', views.year_archive),
...
]

  re_path则用正则表达式来匹配url和截取参数。例如:

# urls.py
from django.urls import path, re_path
from . import views
urlpatterns = [
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.article_detail),
] # views.py
from django.shortcuts import HttpResponse def year_archive(request, year):
return HttpResponse(year)
def month_archive(request, year, month, name):return HttpResponse("%r, %r" % (year, month))
def article_detail(request, year, month, slug, name):return HttpResponse("%r, %r, %r" % (year, month, slug))

  3、view参数

  path源码可以接收的view参数包括: 函数,被URLPattern处理;列表或元组,被URLResolver。view参数也有两个功能,调用视图函数并传递给其参数,以及拆包。

from django.urls import include, path
# 方法一:分别导入属视图函数和urlpatterns(extra_patterns),在urls.py中使用include()函数组合起来from credit import views as credit_views
extra_patterns = [
path('reports/', credit_views.report),
path('reports/<int:id>/', credit_views.report),
path('charge/', credit_views.charge),
]
urlpatterns = [
path('help/', include('apps.urls')), # 方法二:直接将urlpatterns写在应用下(apps/urls.py),urls.py中用include导入apps/urls.py即可
path('credit/', include(extra_patterns)),
]

  来看一下include源码:

def include(arg, namespace=None):
app_name = None
if isinstance(arg, tuple):
# Callable returning a namespace hint.
try:
urlconf_module, app_name = arg
except ValueError:
if namespace:
raise ImproperlyConfigured(
'Cannot override the namespace for a dynamic module that '
'provides a namespace.'
)
raise ImproperlyConfigured(
'Passing a %d-tuple to include() is not supported. Pass a '
'2-tuple containing the list of patterns and app_name, and '
'provide the namespace argument to include() instead.' % len(arg)
)
else:
# No namespace hint - use manually provided namespace.
urlconf_module = arg if isinstance(urlconf_module, str):
urlconf_module = import_module(urlconf_module)
patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
app_name = getattr(urlconf_module, 'app_name', app_name)
if namespace and not app_name:
raise ImproperlyConfigured(
'Specifying a namespace in include() without providing an app_name '
'is not supported. Set the app_name attribute in the included '
'module, or pass a 2-tuple containing the list of patterns and '
'app_name instead.',
)
namespace = namespace or app_name
# Make sure the patterns can be iterated through (without this, some
# testcases will break).
if isinstance(patterns, (list, tuple)):
for url_pattern in patterns:
pattern = getattr(url_pattern, 'pattern', None)
if isinstance(pattern, LocalePrefixPattern):
raise ImproperlyConfigured(
'Using i18n_patterns in an included URLconf is not allowed.'
)
return (urlconf_module, app_name, namespace)

  它可以传入文件路径字符串或者一个包含多个元组的列表(触发else:urlconf_module = arg),并使用importlib.import_module导入文件,也可以传递一个当一个请求进来时,通过反射调用相应的视图函数(pattern = getattr(url_pattern, 'pattern', None))。

  4、path参数类型和作用域

  path函数的参数分为三种:kwargs、route和request。尽管request不属于path,这里为了比较姑且这样写。

  kwargs参数作用域最大,不仅涉及include的所有子路由,而且涉及所有能被route捕捉和匹配的当前路由。kwargs设定的参数需要属兔函数设置同名形参来接收。一般用于后台设置。

# urls.py
from django.contrib import admin
from django.urls import re_path, path, include
from app01 import views
extra_pattern = [
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.article_detail),
]
urlpatterns = [
path('admin/', admin.site.urls),
path('app01/', include(extra_pattern), {"name": "Jan"}),
# path('app01/', include('app01.urls'))
] # app01/views.py
from django.shortcuts import HttpResponse
# 每一个子路由对应的视图函数都要声明name参数
def year_archive(request, year, name):
return HttpResponse("{}, {}".format(year, name))
def month_archive(request, year, month, name):
print(name)
return HttpResponse("%r, %r" % (year, month))
def article_detail(request, year, month, slug, name):
print(name)
return HttpResponse("%r, %r, %r" % (year, month, slug))

  route参数是匹配符合规则的url,并从url中获取参数。它的作用域为这些符合规则的url,并且只影响一个视图函数。

  kwargs和route所设置的参数,都是需要视图函数声明。request参数可以接收GET和POST请求,它需要在视图函数中作为第一个参数声明。request在url之前已经封装好了。

二、视图函数

  1、django.shortcuts

  该模块收集了常见的response工具函数,用于快速的完成视图函数。

  1.render函数

def render(request, template_name, context=None, content_type=None, status=None, using=None):
"""
Return a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
content = loader.render_to_string(template_name, context, request, using=using)
return HttpResponse(content, content_type, status)

  content_type指定文档的MIME类型,status指定状态码,using参数用于指定加载模板的模板引擎。

from django.shortcuts import render
def my_view(request):
return render(request, 'myapp/index.html', {'foo': 'bar'}, content_type='application/xhtml+xml')

  它相当于:

from django.http import HttpResponse
from django.template import loader def my_view(request):
t = loader.get_template('myapp/index.html')
c = {'foo': 'bar'}
return HttpResponse(t.render(c, request), content_type='application/xhtml+xml')

  2、redirect函数

def redirect(to, *args, permanent=False, **kwargs):
"""
Return an HttpResponseRedirect to the appropriate URL for the arguments
passed.
The arguments could be:
* A model: the model's `get_absolute_url()` function will be called.
* A view name, possibly with arguments: `urls.reverse()` will be used
to reverse-resolve the name.
* A URL, which will be used as-is for the redirect location.
Issues a temporary redirect by default; pass permanent=True to issue a
permanent redirect.
"""
  # HttpResponsePermanentRedirect和HttpResponseRedirect在django.http模块中
redirect_class = HttpResponsePermanentRedirect if permanent else HttpResponseRedirect
return redirect_class(resolve_url(to, *args, **kwargs))

  redirect的三种重定向方式:接收参数为一个model并且它实现了get_absolute_url方法;接收一个django.urls.reverse通过视图函数反向生成的url;直接接收重定向的url路径。

# views.py
from django.shortcuts import redirect, HttpResponse, HttpResponseRedirect
from django.urls import reverse class Person:
@staticmethod
def get_absolute_url():
return reverse('icon_handler', kwargs={"name": "Jan"}) def items_handler(request):
# return redirect("/app01/icon") # 返回一个url
# return HttpResponseRedirect(reverse('icon_handler', kwargs={"name": "Jan"})) # 返回一个reverse
return redirect(Person) # 返回一个Model def icon_handler(request, name):
return HttpResponse(name) # urls.py
from django.urls import path
from app01 import views
urlpatterns = [
path('items', views.items_handler),
path('icon/<name>', views.icon_handler, name='icon_handler'),
]

  3、get_object_or_404和get_list_or_404

from django.shortcuts import get_object_or_404

def my_view(request):
my_object = get_object_or_404(MyModel, pk=1)
from django.shortcuts import get_list_or_404
def my_view(request):
my_objects = get_list_or_404(MyModel, published=True)

 

Django(二):url和views的更多相关文章

  1. Django (二) url 和 模板

    1. URL URL地址说明: 使用url给视图函数传参数 在url配置中将正则部分小括号括起来.比如: url(r'^time/plus/(\d{1,2})/$', views.hours_ahea ...

  2. Django 路由系统URL 视图views

    一.Django URL (路由系统) URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Djan ...

  3. python django基础二URL路由系统

    URL配置 基本格式 from django.conf.urls import url #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数执行,就不再往下循环了,并给函数 ...

  4. django中url路由配置及渲染方式

    今天我们学习如何配置url.如何传参.如何命名.以及渲染的方式,内容大致有以下几个方面. 创建视图函数并访问 创建app django中url规则 捕获参数 路径转换器 正则表达式 额外参数 渲染方式 ...

  5. Django的url控制器

    一 url配置 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表.也就是ur ...

  6. Django的URL路由系统

    一. URL配置 URL配置就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图之间的映射表.你就是以这种方式告诉Django,对于哪个URL调用的这段代码. 基本格式 from ...

  7. Django之URL路由系统

    一 URL配置 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表.你就是以这 ...

  8. Django之URL控制器(路由层)

    url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), 一.视图层路由配置系统 URL配置(URLconf)就像Django ...

  9. Django框架_URLconf、Views、template、ORM

    目录: 一.Django-MTV MTV模型 Django基本命令 视图层之路由配置系统(views) 视图层之视图函数(views) 模板层(template) 二.Django-model基础 O ...

  10. Django 02 url路由配置及渲染方式

    Django 02 url路由配置及渲染方式 一.URL #URL #(Uniform Resoure Locator) 统一资源定位符:对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是 ...

随机推荐

  1. Python(模块&包)

    参考:https://www.cnblogs.com/yuanchenqi/articles/5732581.html 在linux下给pycharm安装第三方库,需要在.bashrc中加: 因为对应 ...

  2. GPS坐标转百度地图坐标

    百度地图提供了相关API:BMap.Convertor.translate, 但是使用上存在部分限制:1.次数限制:2.异步回调 可以用如下方法: /** * 地图位置计算工具(将GPS坐标转换成百度 ...

  3. js01--简介、注释、数组、对象、null与undefined

    javascript简介:js,轻量级的脚本语言,插入HTML页面中,用来实现网页的动态交换. 1.js的使用: 写入HTML输出:document.write("<p>This ...

  4. Ubuntu 16.04防火墙

    防火墙(ufw) 说明:简单版本的防火墙,底层依赖于iptables. 安装:sudo apt-get install ufw 查看状态:sudo ufw status 开启/关闭:sudo ufw ...

  5. abp angular 前端权限控制

    import { AppComponentBase } from '@shared/app-component-base'; this.permission.isGranted(menuItem.pe ...

  6. *args and **kwargs

    首先要知道, 并不是必须写成*args 和**kwargs. 只有变量前面的 *(星号)才是必须的. 你也可以写成*var 和**vars. 而写成*args 和**kwargs只是一个通俗的命名约定 ...

  7. 用Docker构建分布式Redis集群

    [编者的话]本文介绍了如何使用Docker搭建Redis集群,很多读者都在问Docker能带来哪些实质性的好处,我想本文就是一个很好的例子.不使用Docker你也可以搭建Redis集群,那使用Dock ...

  8. springboot项目:Redis缓存使用

    保存Redis 第一步:启动类中加入注解 @EnableCaching package com.payease; import org.springframework.boot.SpringAppli ...

  9. Hibernate中Query.list()方法报IllegalArgumentException异常

    最近在使用Hibernate开发项目,在写好hql语句,并初始化Query对象,执行Query.list()方法时,应用报IllegalArgumentException异常.经网上查询,现已经基本决 ...

  10. 【Kafka】Broker之Server.properties的重要参数说明

    名称 描述 类型 默认值 有效值区间 重要程度 zookeeper.connect zk地址 string 高 advertised.host.name 过时的:只有当advertised.liste ...