django shortcuts 详解
django中为了方便web的开发、定义了许多有用的shortcut 。由于shortcut带来的方便用文字描述是有点苍白的、所以在这里打算用一些例子来说明
一、一个hello world 的例子:
我们用low B 的方式实现一个功能,这个功能就是当用户打开页面的时候,向用户显示一句 "hello xxx"
比如我们在url中给出的名字是welson

如果 low B 的实现这个功能 view的代码如下:
from django.shortcuts import render
from django.http import HttpRequest,HttpResponse,HttpResponseRedirect
# Create your views here. from django.shortcuts import render def index(request,name='world'):
html_str="""
<html>
<head>
<title> hello page </title>
</head> <body>
<p>
hello {0}
</p>
</body>
</html>
""".format(name)
return HttpResponse(html_str)
注册url于view的关联(polls.urls.py):
from django.contrib import admin
from django.urls import path
from .views import index urlpatterns = [
path('<str:name>/', index),
]
根url配置如下:
from django.contrib import admin
from django.urls import path,include urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
这种low B 的方式有什么问题:
1、由于html代码与python代码混合在一起、不方便更改;就是给web加一个样式,也是非常困难的,鬼知道你是不是哪个view中
的html代码没有加入新的样式。
二、用template 来提升B格:
创建template文件来,分离html与python
polls/templates/polls/index-template.html的内容如下:
<html>
<head>
<title>this is index template</title>
</head> <body>
<p>
{{ name }}
</p>
</body>
</html>
新的view代码就可以这样写了:
from django.shortcuts import render
from django.http import HttpRequest,HttpResponse,HttpResponseRedirect
# Create your views here. def index(request,name='world'):
return render(request,template_name='polls/index-template.html',context={'name': name})
----
django shortcuts 详解的更多相关文章
- Django 2.0 学习(20):Django 中间件详解
Django 中间件详解 Django中间件 在Django中,中间件(middleware)其实就是一个类,在请求到来和结束后,Django会根据自己的规则在合适的时机执行中间件中相应的方法. 1. ...
- Django -- settings 详解
Django settings详解 1.基础 DJANGO_SETTING_MODULE环境变量:让settings模块被包含到python可以找到的目录下,开发情况下不需要,我们通常会在当前文件夹运 ...
- Django -- settings 详解(转)
Django -- settings 详解 Django settings详解 1.基础 DJANGO_SETTING_MODULE环境变量:让settings模块被包含到python可以找到的目 ...
- Django中间件详解
Django中间件详解 中间件位置 WSGI 主要负责的就是负责和浏览器和应用之家沟通的桥梁 浏览器发送过来一个http请求,WSGI负责解包,并封装成能够给APP使用的environ,当app数据返 ...
- Django路由详解
一.路由基础 1.路由url函数:路由自上而下进行匹配:url(正则路径,视图函数内存地址,默认参数,路由别名) 2.路由正则: 规定开始:^ | 规定结束:$ #url(r'index', view ...
- Django ORM详解
ORM:(在django中,根据代码中的类自动生成数据库的表也叫--code first) ORM:Object Relational Mapping(关系对象映射) 我们写的类表示数据库中的表 我们 ...
- Django框架详解
一.WSGI接口 WSGI服务网关接口:Web Server Gateway Interface缩写. WSGI是python定义的Web服务器和Web应用程序之间或框架之间的通用接口标准. WSGI ...
- django http404 详解
[引子] 今天在看django的官方文档的时候看到get_object_or_404这个函数感觉比较奇怪.这个主要来自于它的功能,如果要查询的对象 存在那么就返回对象:如果对象不存在那么就要报404 ...
- Python全栈之路--Django ORM详解
ORM:(在django中,根据代码中的类自动生成数据库的表也叫--code first) ORM:Object Relational Mapping(关系对象映射) 我们写的类表示数据库中的表 我们 ...
随机推荐
- Python 字典(联合内存、联合数组)
字典 Python有一个内建数据类型是字典(Dictionaries).字典在某些语言中可能称为“联合内存”("associative memories'')或“联合数组”("as ...
- Java之所有对象的公用方法>8.Obey the general contract when overriding equals
Overriding the equals method seems simple, but there are many ways to get it wrong, and consequences ...
- eclipse svn新增文件不显示在文件列表,只有修改文件可以提交!
1.情景展示 eclipse修改的文件可以正常提交,但是新增的文件没有显示在提交列表中,导致无法提交! 2.解决方案 选中要提交的文件-->右键-->Team-->提交 勾选上这 ...
- 【Linux】文件夹及作用说明
Tips Linux关机注意事项: 远程重启服务前,先停止相关服务 使用安全命令重启shutdown –r now,该命令在重启时会正常保存和终止服务器上正在运行的程序 不建议在本地直接对远程服务器关 ...
- Dlib三维点云示例
Dlib三维点云示例 源代码来自Dlib的示例代码http://dlib.net/3d_point_cloud_ex.cpp.html 在windows下需要链接winmm``comctl32``gd ...
- 转 安装PHP出现make: *** [sapi/cli/php] Error 1 解决办法
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_ctor':/home/king/php-5.2.13/ext/iconv/ ...
- nginx常用超时设置
客户度连接nginx超时, 建议5s内 接收客户端header超时, 默认60s, 如果60s内没有收到完整的http包头, 返回408 Syntax: client_header_timeout t ...
- 自定义nsoperation的用法
#import <Foundation/Foundation.h> typedef void (^DownloadCompletionBlock)(UIImage *image); @in ...
- ubuntu 安装 oracle-xe-universal
安装oracle-xe-universal第一个我们要考虑的就是交换分区是否足够大, 如果你直接安装,可能会出现下面的英文提示: This system does not meet the minim ...
- 统计bytearray中的bitcount
给定一个byte数组,要求统计byte数组的bitcount,也就是byte数组中为1的位的个数. Redis提供了位数组数据结构,位数组是相对独立的一个程序,在<Redis设计与实现>( ...