安装与验证
安装前提安装了Python3 pip等基础环境
sudo pip3 install Django==1.8.13
成功后编写第一个Demo
mkdir django
cd django
django-admin startproject HelloWorld
cd HelloWorld
python3 manage.py startapp Hello
编辑 Hello 目录中的 views.py 文件如下:
#coding: utf-8
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse(u'Hello World')
编辑 ./HelloWorld/settings.py,在 INSTALLED_APPS 这个数组中加入我们的 app:
NSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Hello',
)
为刚刚的页面配置 URL 路由即可完成,要编辑的文件为 ./HelloWorld/urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from Hello import views as Hello_views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', Hello_views.index)
]
启动server
$ python3 manage.py runserver 8080
访问 http://127.0.0.1:8080
网友评论