您当前的位置: 首页 >  django

05 Django入门 - 视图(介绍)

杨林伟 发布时间:2019-08-28 09:39:58 ,浏览量:3

在django中,视图对WEB请求进行回应

视图接收reqeust对象作为第一个参数,包含了请求的信息

视图就是一个Python函数,被定义在views.py中

#coding:utf-8
from django.http import HttpResponse

def index(request):
    return HttpResponse("index")
def detail(request,id):
    return HttpResponse("detail %s" % id)

在这里插入图片描述 在这里插入图片描述

定义完成视图后,需要配置urlconf,否则无法处理请求

URLconf

在Django中,定义URLconf包括正则表达式、视图两部分

Django使用正则表达式匹配请求的URL,一旦匹配成功,则调用应用的视图

注意:只匹配路径部分,即除去域名、参数后的字符串

在BookProject/urls.py插入booktest,使主urlconf连接到booktest.urls模块

url(r'^', include('booktest.urls')),

在booktest中的urls.py中添加urlconf

from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^$', views.index),
    url(r'^([0-9]+)/$', views.detail),
]
关注
打赏
1688896170
查看更多评论

杨林伟

暂无认证

  • 3浏览

    0关注

    3183博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0694s