django
python
一、创建django项目先创建一个项目为testweb,创建一个应用为hello在dos的命令如下E:\>django-admin-script.py startproject testwebE:\>cd testwebE:\testweb>django-admin-script.py startapp hello注意命令和字母大小写
二、修改testweb的settings.py的参数找到INSTALLED_APPS项,在此项添加hello如下:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'hello',]
三、定义hello的view的视图函数打开testweb,打开hello文件夹,找到viewspy修改如下:#coding:utf-8from django.http import HttpResponsefrom django.shortcuts import render def index(request): return HttpResponse('欢迎打开django的大门')注意:python的缩进问题
四、修改testweb的URL参数打开testweb的子目录testweb,找到urls.py修改如下from django.conf.urls import urlfrom django.contrib import adminfrom hello import views as hello_viewsurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$',hello_views.index),]
五、检查结果和查看进入DOS下,打开testweb目录,运行命令manage.py runserver如下:E:\django\testweb>manage.py runserverPerforming system checks...System check identified no issues (0 silenced).You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.Run 'python manage.py migrate' to apply them.February 02, 2017 - 21:26:34Django version 1.10.4, using settings 'testweb.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CTRL-BREAK.[02/Feb/2017 21:27:05] 'GET / HTTP/1.1' 200 27得到以下结果后在浏览器打开:
注意python代码的缩进问题,查看中英文字符串的问题