用Django创建一个hello world

编辑urls.py,在urlpatterns = patterns('',前面加入以下代码:

 

from django.http import HttpResponse

def hello(request):
    return HttpResponse('hello, world!')

 

然后在urlpatterns = patterns('',中加入以下代码:

 

url(r'^$', hello),

 

最终的urls.py看起来如下:

 

from django.conf.urls import patterns, include, url
from django.http import HttpResponse

def hello(request):
    return HttpResponse('hello, world!')

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

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'hello.views.home', name='home'),
    # url(r'^hello/', include('hello.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)),
      url(r'^$', hello),
)

 

启动服务,访问http://localhost:8000/即可看到hello world了。

 

这种实现方式并不推荐,如果要创建基于MVC的Django项目,请参考:创建一个简单的基于MVC的Django项目

你可能感兴趣的:(python,django)