进入到项目要建立的目录
zhangdeMacBook-Pro:~ zhang$ cd Documents/Django
创建项目(project)
zhangdeMacBook-Pro:Django zhang$ django-admin.py startproject learn
进入项目
zhangdeMacBook-Pro:Django zhang$ cd learn
创建app
zhangdeMacBook-Pro:learn zhang$ django-admin.py startapp blog
修改settings.py文件,添加app
zhangdeMacBook-Pro:learn zhang$ mvim learn/settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', #new
)
修改urls.py文件
zhangdeMacBook-Pro:learn zhang$ mvim learn/urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/index/$','blog.views.index'), #new
]
index是一个方法,存放于blog/views.py中。
编辑blog/views.py文件
zhangdeMacBook-Pro:learn zhang$ mvim blog/views.py
from django.shortcuts import render
from django.http import HttpResponse #new
def index(): #new
return HttpResponse('Hello World!
')
试运行
zhangdeMacBook-Pro:learn zhang$ python3 manage.py runserver
用本机服务器。。
成功则显示:
打开浏览器
输入:
127.0.0.1:8000/blog/index
会显示出
Hello World!
至此,第一个hello world程序就成功了。