您的位置:首页 > 编程语言 > Go语言

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

2015-05-29 13:22 465 查看
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 网页显示