一、新建一个django项目bester:
django-admin startproject bester
二、在bester项目中建一个叫polls的应用程序:
cd bester/python3 manage.py startapp polls
三、给polls加一个叫index的函数:
cat polls/views.py
from django.shortcuts import renderfrom django.http import HttpResponsedef index(requst): return HttpResponse("hello world ....")
四、polls增加路由:
1、在polls项目中加一个urls.py的文件、里面维护了一组url到视图的对应关系
touch polls/urls.py
2、polls/urls.py文件的内容如下:
from django.conf.urls import urlfrom .views import indexurlpatterns=[ url(r'^$',index),]
五、把polls/urls.py中的路由信息增加到项目的路由信息中去:
修改bester/urls.py 文件的内容如下:
"""bester URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/Examples:Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))"""from django.conf.urls import url,includefrom django.contrib import adminurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^polls/',include('polls.urls'))]
六、启动http服务:
python3 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.November 13, 2017 - 08:43:42Django version 1.11.2, using settings 'bester.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.
七、从浏览器打开网页:
总结:
以上内容就是django官方文档中的第一个入门的例子了。
-----