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(关系对象映射) 我们写的类表示数据库中的表 我们 ...
随机推荐
- SHELL异常处理
写SHELL好久了,经常被异常困扰,可竟然坚持了若干年没用过,回想以前服务过的公司,阿弥陀佛,罪过罪过.废话少说,希望此篇文章可以协助大家和我彻底结束SHELL脚本就是LINUX命令集合的初级阶段. ...
- Git如何获得两个版本间所有变更的文件列表
https://segmentfault.com/q/1010000000133613 git diff --name-status HEAD~2 HEAD~3
- anagrams 查找序列里具有相同字符但顺序不同的单词
[LeetCode] Anagrams Given an array of strings, return all groups of strings that are anagrams. Not ...
- Oracle单引号双重角色——字符串引用与转义
单引号双重角色——字符串引用与转义 在ORACLE中,单引号有两个作用,一是字符串是由单引号引用,二是转义,开始学习时动态构成SQL时经常不解.单引号的使用是就近配对,即就近原则.而在单引号 ...
- 解决ajax异步传输数据,return返回为undefined的问题
function GetUserInfo(tp) { var username; $.ajax({ type: "POST", cache: false, data: " ...
- C#让控制台程序不显示闪退窗口的方法
新建一个控制台程序,然后编译为 窗体程序.即可........
- A. Kyoya and Photobooks(Codeforces Round #309 (Div. 2))
A. Kyoya and Photobooks Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He ...
- apache安装mod_ssl.so 出现 undefined symbol: ssl_cmd_SSLPassPhraseDialog错误解决
很久很久以前,安装Apache的时候,根本没想过将来的某一天会使用到ssl,所以也就没有安装那个模块,结果今天需要用到的时候,却无从下手了. 由于在安装Apache的时候,mod_ssl.so这个文件 ...
- 如何查看出口IP地址?
出口ip地址怎么看?#curl ifconfig.me
- CreateThread与_beginthreadex本质区别
函数功能:创建线程 函数原型: HANDLEWINAPICreateThread( LPSECURITY_ATTRIBUTESlpThreadAttributes, SIZE_TdwStackSize ...