在django中添加简单视图

时间:2013-04-10 15:57:16

标签: django django-views

我开始使用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 个答案:

答案 0 :(得分: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)),
)