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(关系对象映射) 我们写的类表示数据库中的表 我们 ...
随机推荐
- JSP与Servlet之间的关系事例说明
Servlet Servlet 没有 main 方法,不能够独立的运行,它的运行需要容器的支持,Tomcat 是最常用的 JSP/Servlet 容器.Servlet 运行在 Servlet 容器中, ...
- SHELL pv uv 统计事例
#!/bin/sh #statistics newplive logs SOURCELOGS=$ ];then echo echo "please input file!" ech ...
- JAVA排序总结
package com.softeem.jbs.lesson4; import java.util.Random; /** * 排序测试类 * * 排序算法的分类如下: * 1.插入排序(直接插入排序 ...
- 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 ...
- 用FireBreath来编写跨浏览器插件
这是对于公司某个需求的临时研究,最后经过简单实验放弃了这个方案,因为编写插件不能满足需求. 下面着重讲一下FireBreath编译. 首先根据文档,用git clone下载Firebreath源码(不 ...
- Windows下的包管理器Chocolatey的使用
1.配置安装路径(后续需要管理员权限执行命令) Create a machine level (user level will also work) environment variable name ...
- 【RS】List-wise learning to rank with matrix factorization for collaborative filtering - 结合列表启发排序和矩阵分解的协同过滤
[论文标题]List-wise learning to rank with matrix factorization for collaborative filtering (RecSys '10 ...
- linux shell 脚本攻略学习18--grep命令详解
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是unix/linux中用于文本搜索 ...
- NBUT [1475] Bachelor
[1475] Bachelor http://acm.nbut.cn:8081/Problem/view.xhtml?id=1475 时间限制: 1000 ms 内存限制: 65535 K 问题描述 ...
- Vue.js hello world
<!DOCTYPE HTML> <html> <head> <title>vue.js hello world</title> <sc ...