在刚组装进行Django后,大家再次来学习培训Django,这节大家来写Django的第一个程序流程hello world。好下边大家逐渐。。。
1. 进到上节大家安裝的Django的文件目录,随后在命令运作如下所示指令:
shell# django-admin.py startproject pythontab.com
那样就建立了一个名字叫做pythontab.com的新项目,看一下文件目录构造:
shell# tree pythontab.com
表明結果:
pythontab.com/
|____ manager.py
|____ Blog/
|____ urls.py
|____ wsgi.py
|____ ._init__.py
|____ settings.py
文档构造中个文档及文件夹名称的表述:
manager.py是开发设计全过程时要经常应用的文档,说白了,便是用于管理方法的文档,例如建立app,运作shell,运行Django内嵌的web服务器这些
urls.py文件是Django URL的环境变量,对于当客户浏览www.example/post/1254/时,Django会依据url.py的信息来分辨这一URL由尝试(views)中那一个函数公式来解决,也就是一个路由器文档
._init__.py这一文档是空的,python的包都是会有一个._init__.py文件。一般不用改动该文件
wsgi.pywsgi是Web服务端网关ip插口(Python Web Server Gateway Interface,简称为WSGI)是Python应用软件或架构和Web服务端中间的一种插口。
settings.py :该 Django 新项目的设定或配备。 查询并了解这一文档中可以用的设定种类以及初始值。
下边逐渐写第一个hello world
开启urls.py文件,随后在文档的最前边添加如下所示编码:
from django.http import HttpResponse def hello(request): return HttpResponse('hello world')
随后在patterns(”"),中添加如下所示编码:
url(r'^$', hello)
详细编码如下所示:
from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() from django.http import HttpResponse def hello(request): return HttpResponse('hello world') urlpatterns = patterns('', # Examples: # url(r'^$', 'Blog.views.home', name='home'), # url(r'^Blog/', include('Blog.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), )
随后储存文档
最终,实行在当前目录下的manager.py文件
shell# ./manager runserver
运作运行后面服务项目
随后大家就可以在网页中浏览啦!http://127.0.0.1:80 就可以见到导出hello world啦!
非常简单吧~~
刚新手入门Django提议阅读文章《新手学习Django的十条注意事项》