from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
This is the simplest view possible in Django. To call the view, we need to mapit to a URL - and for this we need a URLconf.
To create a URLconf in the polls directory, create a file called urls.py
.Your app directory should now look like:
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.py
2.request实际上是接受到了客户端(浏览器吧)发过来的请求,比如你想看电影,就点击了电影按钮,这个按钮就是一个request请求
3.request请求被路由转发到指定的函数(方法),从上面可以看出这个函数返回了一个HttpResponse响应
这个HttpResponse肯定是返回信息,对吧,如果说人对服务器做出请求是一个request,那么服务器对人做出响应就是response
那就是HttpRespnse就是把信息输出到客户端(浏览器)
4.request和response是怎么协作的,就是通过协议,这个协议是URL地址,就是上文中的URLconf
5.现在我们知道了需要在django的一个具体的APP中创建一个urls.py文件来负责通信
这个主要涉及了views.py和urls.py, 并且涉及到了HttpRequest和HttpResponse对象
In the polls/urls.py
file include the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
这个里这要是强调下url函数的写法
jango.conf.urls.include
and insertan include()
in the urlpatterns
list, so you have:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
主要是强调把app的urls.py引用到了主目录下的urls.py文件
流程
1.在views.py中写上服务器可能要返回给客户端的内容
2.在app中创建urls.py文件,把具体的函数写入到urls.py中
3.把app中的urls.py导入主目录下的urls.py