#### 配置URL
1. 项目包/urls.py
```
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
url(r‘^student/‘, include(‘student.urls‘)),
]
```
2. 应用包/urls.py
```
#coding=utf-8
from django.conf.urls import url
import views
urlpatterns=[
url(r‘^query1/$‘,views.query_view1)
]
```
#### 创建视图
- 方式1:
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from django.template import Template,Context
# Create your views here.
def query_view1(request):
t = Template(‘hello:{{name}}‘)
c = Context({‘name‘:‘zhangsan‘})
renderStr = t.render(c)
return HttpResponse(renderStr)
```
- 方式2:
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from django.template import Template,Context
# Create your views here.
def query_view1(request):
with open(‘templates/index.html‘,‘rb‘) as fr:
content = fr.read()
t = Template(content)
c = Context({‘name‘:‘lisi‘})
renderStr = t.render(c)
return HttpResponse(renderStr)
```
#### 创建模板
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello:{{ name }}
</body>
</html>
```
- 方式3:
#### 创建视图
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from django.template import Template,Context
from django.shortcuts import loader
# Create your views here.
def query_view1(request):
t = loader.get_template(‘index.html‘)
renderStr = t.render({‘name‘:‘wangwu‘})
return HttpResponse(renderStr)
```
- 方式4:
#### 配置视图
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def query_view1(request):
return render(request,‘index.html‘,{‘name‘:‘zhaoliu‘})
```
五、自定义过滤器
#### 配置URL
```
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
url(r‘^student/‘, include(‘student.urls‘)),
]
#coding=utf-8
from django.conf.urls import url
import views
urlpatterns=[
url(r‘^$‘,views.index_view)
]
```
#### 创建视图
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
def index_view(request):
content = ‘‘‘####过滤器‘‘‘
return render(request,‘index.html‘,{‘content‘:content})
```
#### 创建模板
```
{% load filter_mark %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ content|md|safe }}
</body>
</html>
```
#### 创建自定义过滤器
1. 在应用包下创建一个名为"templatetags"的python package
2. 在包中创建一个自定义的py文件
pip install markdown
```
#coding=utf-8
from django.template import Library
#实例名必须是register
register = Library()
@register.filter
def md(value):
import markdown
return markdown.markdown(value)
```
#### 截取字符串功能
```
#coding=utf-8
from django.template import Library
register = Library()
@register.filter
def splitstr(value,args):
start,end = args.split(‘,‘)
content = value.encode(‘utf-8‘).decode(‘utf-8‘)
return content[int(start):int(end)]
```
- index.html页面
```
{% load filter_mark %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ content|splitstr:‘1,20‘ }}
</body>
</html>
```