小菜一枚,做得不科学的地方请多多指出~
Djnago版本:1.6
1、创建项目:
1
|
django
-
admin.py startproject QjshAdmin
|
进入QjshAdmin目录,运行测试服务端:
1
|
manage.py runserver
8000
|
访问127.0.0.1:8000能正常显示
2、新建APP:
1
|
python manage.py startapp SaAdmin
|
配置QjshAdmin/settting.py
1
2
3
4
5
6
7
8
9
|
INSTALLED_APPS
=
(
'django.contrib.admin'
,
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'SaAdmin'
,
)
|
配置QjshAdmin/urls.py
1
2
3
4
5
6
7
8
|
urlpatterns
=
patterns('',
# Examples:
# url(r'^$', 'QjshAdmin.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r
'^admin/'
, include(admin.site.urls)),
url(r
'^saadmin/'
, include(
'SaAdmin.urls'
)),
)
|
将saadmin开头的url解析指向SaAdmin这个APP的urls,然后写个简单的测试页面:
SaAdmin/urls.py
1
2
3
4
5
6
|
from
django.conf.urls
import
patterns, include, url
from
django.views.generic
import
TemplateView
urlpatterns
=
patterns('',
url(r
'^test/$'
, TemplateView.as_view(template_name
=
'test.html'
)),
)
|
在SaAdmin目录下新建一个templates目录存放模板:
SaAdmin/templates/test.html
1
2
3
4
5
6
7
8
9
|
<!DOCTYPE html>
<
html
>
<
head
>
<
title
></
title
>
</
head
>
<
body
>
测试页面
</
body
>
</
html
>
|
启动服务器,打开URL:http://127.0.0.1:8000/saadmin/test/
3、安装django-jinja,使django支持jinja2模板
django-jinja文档:http://niwibe.github.io/django-jinja/
下载回来解压安装,复制django_jinja至项目下,和manage.py同一层
配置settings.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
INSTALLED_APPS
=
(
'django.contrib.admin'
,
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'SaAdmin'
,
'django_jinja'
,
)
......
#django_jinja
TEMPLATE_LOADERS
=
(
'django_jinja.loaders.AppLoader'
,
'django_jinja.loaders.FileSystemLoader'
,
)
DEFAULT_JINJA2_TEMPLATE_EXTENSION
=
'.jinja.html'
|
django_jinja将自动解析后缀为.jinja.html的文件,再写一个测试页面:
urls.py
1
2
3
|
urlpatterns
=
patterns('',
url(r
'^test/$'
, TemplateView.as_view(template_name
=
'test.jinja.html'
)),
)
|
test.jinja.html
1
2
3
4
5
6
7
8
9
10
11
|
<!DOCTYPE html>
<
html
>
<
head
>
<
title
></
title
>
</
head
>
<
body
>
测试Jinja2页面:
21模4的值是:{{ 21 % 4 }}
</
body
>
</
html
>
|
django自带的模板是不支持模运算,我们看看结果:
能够进行模运算,jinja2模板系统的功能是正常的
4、最后再添加多一个setting.py的配置:
1
2
3
4
5
6
7
8
9
10
|
TEMPLATE_CONTEXT_PROCESSORS
=
(
"django.core.context_processors.request"
,
#add
"django.contrib.auth.context_processors.auth"
,
"django.core.context_processors.debug"
,
"django.core.context_processors.i18n"
,
"django.core.context_processors.media"
,
"django.core.context_processors.static"
,
"django.core.context_processors.tz"
,
"django.contrib.messages.context_processors.messages"
,
)
|
作用是模板中可以用request变量
环境准备到这里
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1435293如需转载请自行联系原作者
lihuipeng