2013-04-10 20 views
0

我入門Django上,這是我的看法是怎麼看起來像添加在Django簡單的觀點

from django.template import Context, loader 
from datetime import datetime 
from django.http import HttpResponse 

def hello_view(request): 
    """ Simple Hello World View """ 
    t = loader.get_template('helloworld.html') 
    c = Context({ 
     'current_time': datetime.now(), 
    }) 
    return HttpResponse(t.render(c)) 

def detail_view(request): 
    return HttpResponse("You're looking at detail view") 

我的urls.py文件看起來像這樣

from django.conf.urls import patterns, include, url 
from posted.views import hello_view 
#from posted.views import detail_view 

# Uncomment the next two lines to enable the admin: 
# from django.contrib import admin 
# admin.autodiscover() 

urlpatterns = patterns('', 
    # Examples: 
    url(r'^$', view=hello_view, name='hello_page'), 
    #url(r'^$', view=detail_view, name='detail_page'), 
    # url(r'^posts/', include('posts.foo.urls')), 

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

    # Uncomment the next line to enable the admin: 
    # url(r'^admin/', include(admin.site.urls)), 
) 

當我運行的服務器並訪問http://example.com:8000/,hello_view得到顯示。我希望我的網址的格式爲http://example.com:8000/hellohttp://example.com:8000/detail。我需要.htaccess來實現這一目標嗎?

+1

不,你爲什麼需要.htaccess? [教程](https://docs.djangoproject.com/en/1.5/intro/tutorial04/#amend-urlconf)詳細介紹了這一點。 – 2013-04-10 15:59:14

+0

你的意思是說你不想跟蹤斜線? – OozeMeister 2013-04-10 16:05:59

回答

0

我已經解決了這個問題,當我研究丹尼爾建議我看的時候。

from django.conf.urls import patterns, include, url 
from django.views.generic import TemplateView 

# Uncomment the next two lines to enable the admin: 
# from django.contrib import admin 
# admin.autodiscover() 

urlpatterns = patterns('', 
    # Examples: 
    (r'^detail/', TemplateView.as_view(template_name="detail.html")), 
    # url(r'^posts/', include('posts.foo.urls')), 

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

    # Uncomment the next line to enable the admin: 
    # url(r'^admin/', include(admin.site.urls)), 
)