初学python的Web框架Django-模板

修改django的url规则和模板的基本操作
We’ll just have to take a few steps to make the conversion. We will:

1. Convert the URLconf.
2. Rename a few templates.
3. Delete some of the old, unneeded views.
4. Fix up URL handling for the new views.


bear@njava:~/njava$ vi urls.py
urlpatterns = patterns('',
    # Example:
    # (r'^tt/', include('tt.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
     (r'^$','tt.news.views.index'),
     (r'^news-(?P\d+).html/$','tt.news.views.detail'),
     (r'^news/$','tt.news.views.index'),
     (r'^admin/', include(admin.site.urls))
)

bear@njava:~/njava$ cd news
bear@njava:~/njava/news$ vi views.py
# Create your views here.

from django.shortcuts import render_to_response,get_object_or_404
#from django.http import HttpResponse
from tt.news.models import Post

def index(request):
    posts = Post.objects.all().order_by('-pub_date')[:5]
    #return HttpResponse("Hello,m44,this index.")
    return render_to_response('news/index.html',{'posts':posts})

def detail(request,post_id):
    p=get_object_or_404(Post, pk=post_id)
    return render_to_response('news/detail.html',{'post':p})

link:http://docs.djangoproject.com/en/1.2/intro/tutorial03/#intro-tutorial03
http://docs.djangoproject.com/en/1.2/intro/tutorial04/#intro-tutorial04

Tags: , ,

星期四, 20 5 月, 2010 Web

一条评论 to 初学python的Web框架Django-模板

Leave a Reply

1LMooBmUE153Wnd3zDryWvDyXxQudbFxDr