Skip to content

Commit

Permalink
django模板
Browse files Browse the repository at this point in the history
  • Loading branch information
张杰 committed Jul 4, 2016
1 parent 9587cd8 commit 2c72e94
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions source/_posts/django/django-创建项目.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,34 @@ urlpatterns = patterns("",
<h1>{{ hello }}</h1>
```

模板文件可以设置多个文件夹,应该也可以整合其他模板引擎,如jinja2,后面做demo尝试一把
模板文件的路径设置,修改HelloWorld/settings.py,修改 TEMPLATES 中的 DIRS 为 [BASE_DIR+"/templates",],如下所示:
```{bash}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR+"/templates",],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
```

### view中使用模板
我们使用了渲染器render,变量从context字典中赋值
```{bash}
#from django.http import HttpResponse
from django.shortcuts import render
def hello(request):
context = {}
context['hello'] = 'Hello World!'
return render(request, 'hello.html', context)
```

0 comments on commit 2c72e94

Please sign in to comment.