首页 > Web开发 > 详细

Django笔记(2)-- 在网页中显示hello world

时间:2015-05-29 16:00:06      阅读:202      评论:0      收藏:0      [点我收藏+]

1. 又见hello world

接上文,在新建的mysite中创建view.py的空文件。在文件中添加代码。

from django.http import HttpResponse

def hello(req):
return HttpResponse("Hello world")

配置url

在新建了一个项目后,项目中会自动生成一个urls.py的文件(URLconf)。默认的文件样式:

from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns(‘‘,
    # Example:
    # (r‘^mysite/‘, include(‘mysite.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‘^admin/‘, include(admin.site.urls)),
)

URLconf 就像是 Django 所支撑网站的目录。 它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间的映射表。 你就是以这种方式告诉 Django,对于这个 URL 调用这段代码,对于那个 URL 调用那段代码。

注释可以去掉,添加如下代码。

from django.conf.urls.defaults import *
from mysite.views import hello
urlpatterns = patterns(‘‘,
(‘^hello/$‘, hello),
)


然后运行命令:python manage.py runserver

打开浏览器访问:http://127.0.0.1:8000/hello/





笔记来自《Djongo中文教程》

Django笔记(2)-- 在网页中显示hello world

原文:http://4440271.blog.51cto.com/4430271/1656291

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!