加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Django 笔记(二) 新建 ~ 渲染

发布时间:2020-12-15 17:18:06 所属栏目:大数据 来源:网络整理
导读:新建APP python manange.py startapp app_name 然后右键 pycharm?的项目目录,将新建的目录从服务器上下载进来 URL(Uniform Resoure Locator)统一资源定位符 格式: http://127.0.0.1:8000/hello/ URL解释: schema://host[:port#]/path/.../[?query-strin

新建APP

python manange.py startapp app_name

然后右键 pycharm?的项目目录,将新建的目录从服务器上下载进来

URL(Uniform Resoure Locator)统一资源定位符

格式:

http://127.0.0.1:8000/hello/

URL解释:

  schema://host[:port#]/path/.../[?query-string][#anchor]

  schema:指定使用的协议(例如:http,https,ftp)

  host:Http服务器的IP地址或者域名

  port:端口号,http默认是80端口

  path:访问资源的路径

  query-string:发送给http服务器的数据

  anchor:锚点

urls.py的作用

path('test//',views.test)

前面的url匹配成功后就会调用后面的视图函数。

尖括号从url中捕获值,包含一个转化器类型(converter?type)。

没有转化器,将匹配任意字符串,当然也包括了 /?字符。

注:?必须与视图函数的参数一致

例:def test(request,xx)

转换器:

str:匹配除了路径分隔符(/)之外的非空字符串,这是默认的形式

int:匹配正整数,包含0

slug:匹配字母、数字以及横杠、下划线组成的字符串

uuid:匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00

path:匹配任何非空字符串,包含了路径分隔符

re_path?正则匹配:

可使用正则的方法匹配

include?的作用:

一个 project 有一个棕的 urls.py ,各个 app 也可以自己建立自己的 urls.py

用 include() 函数在project的 urls.py?文件进行注册

例:

django.urls <span style="color: #0000ff;">from . <span style="color: #0000ff;">import<span style="color: #000000;"> views

urlpatterns =<span style="color: #000000;"> [

  path(<span style="color: #800000;">'<span style="color: #800000;">book/<span style="color: #800000;">',include(<span style="color: #800000;">'<span style="color: #800000;">book.urls<span style="color: #800000;">'<span style="color: #000000;">))

]

?此时 APP?books?里面的 urls.py

django.urls <span style="color: #0000ff;">from . <span style="color: #0000ff;">import<span style="color: #000000;"> views

urlpatterns =<span style="color: #000000;"> [

  path(<span style="color: #800000;">'<span style="color: #800000;">index/<span style="color: #800000;">'<span style="color: #000000;">,views.index)

]

此时 APP?books?里面的?views.py

django.shortcuts <span style="color: #0000ff;">def<span style="color: #000000;"> index(request):

  <span style="color: #0000ff;">return render(request,<span style="color: #800000;">'<span style="color: #800000;">这是 book 的首页<span style="color: #800000;">')

kwargs 的作用:

不定长参数

如果在分 urls.py 添加了字典参数

path(,views.test,{‘switch’: })

views?下面的 test 函数形参需要额外增加一个 **kwargs

如果在使用了 include ,?主 views?添加了?字典参数

其分之下所有函数形参都需要添加 **kwargs

name?的作用:

可以给 url?取名字,通过该名字找到对应?url,这样左的原因是防止 url?的规则更改,导致使用了该 url?的地方也要更改,但如去了名字,就不要做任何改动了。

APP?books里的 urls.py

path(,views.article_new,name=)

APP?books?里的?views.py

django.shortcuts <span style="color: #0000ff;">return redirect(reverse(<span style="color: #800000;">'<span style="color: #800000;">new_article<span style="color: #800000;">'))

注:redirect?是重定向(跳转),?reverse?是将 url?的 name?解析成 url?本身的函数

templates 模板:

该模板可新建各个以 app?命名的目录存放各自 app?模板文件

然后在 setting.py?中的模板路径配置修改以下内容

TEMPLATES = [

  'DIRS': [os.path.join(BASE_DIR,'templates')],

]

渲染模板(三种方法选一,皆需 import 导入才能使用):

1.直接将html字符串硬编码 HttpResponse?中

  <span style="color: #0000ff;">return HttpResponse(<span style="color: #800000;">'<span style="color: #800000;">

Hello Django World!

<span style="color: #800000;">')

2. django.template.loader?定义了函数以加载模板

django.template.loader <span style="color: #0000ff;">def<span style="color: #000000;"> index(request):

  t = get_template(<span style="color: #800000;">'<span style="color: #800000;">book/index.html<span style="color: #800000;">'<span style="color: #000000;">)

  html =<span style="color: #000000;"> t.render()

  <span style="color: #0000ff;">return HttpResponse(html)

3.使用 render?进行渲染(建议此方法)

  <span style="color: #0000ff;">return render(request,<span style="color: #800000;">'<span style="color: #800000;">book/index.html<span style="color: #800000;">')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读