2013-08-07 20:02:10|
1.新建blog的URL文件
在blog目录下新建文件"urls.py" :
# -*- coding: utf-8 -*-
from django.conf.urls import patterns, include, url
urlpatterns = patterns(‘blog.views‘, # 提取出一个公共视图前缀
url(r‘blogs/$‘, ‘show_blogs‘), # 第二个参数指向视图
)
2.包含其他URL
在
"mysite\urls.py"文件中包含blog的url:
# -*- coding: utf-8 -*-
from django.conf.urls import patterns,
include, url
from django.contrib import
admin
admin.autodiscover()
urlpatterns =
patterns(‘‘,
url(r‘^admin/‘,
include(admin.site.urls)),
url(r‘^blog/‘,
include(‘blog.urls‘)), # 包含blog的URL
)
3. 创建视图list
修改文件"blog\views.py":
# -*- coding: utf-8 -*-
from django.shortcuts import
render_to_response
from django.template import
RequestContext
from blog.models import Group,
Article
def show_blogs(request):
"""显示所有日志"""
group_list =
Group.objects.all()
article_list =
Article.objects.all()
return
render_to_response(‘list.html‘, {
‘group_list‘: group_list,
‘article_list‘: article_list,
},
context_instance=RequestContext(request))