模板、URL、同一个项目其他文件配置

文件目录:

1.同一个项目其他文件配置:

在文件:settings.py中的这里面添加

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myTest',    #这是新添加的文件
)

 

2.模板配置

(1)添加模板文件夹路径:添加myTest下的templates模板文件(其中千万把‘templates’命名为template,因为在django中已经存在,相同时,自己添加的模板文件没生效,访问时会报错,找不到相应的文件)

在settings.py中添加:

from os.path import join
TEMPLATE_DIRS = (
join(os.path.dirname(__file__), 'myTest\\templates'),
)

使用:在views.py文件中例如:

# 加载模板
from django.shortcuts import render_to_response, render
def template_test(request):
# return render(request, '2.html', {'name':'hello'})
return render_to_response('2.html', {'name':'test_template'})

 

3.URL配置:

如在web中访问地址http://localhost:8000/hello/test1/

在URL中添加:

from myTest.views import *

urlpatterns = [
url(r'^hello/$', hello),
url(r'^hello/(\d+)/', hello1),
url(r'^hello/a/', template_test),    
url(r'^hello/test1/', template_test1),  # 访问的时匹配的是这行
url(r'^admin/', include(admin.site.urls)),
]

template_test1是myTest.views里面的函数如下:

def template_test1(request):
# return render(request, '2.html', {'name':'hello'})
return render_to_response('4.html')

 

posted @ 2015-07-12 00:17  lanselanlan  阅读(166)  评论(0编辑  收藏  举报