Django

polls/views.py
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
1.request这实际上是HttpRequest的一个实例,看到别人的文章说这个HttpRequest是Django主动创建的

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:

polls/urls.py
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:

mysite/urls.py
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值